Your task is to make a program that decides if a real number between 0 and 1 is irrational or not. As stated, this is obviously impossible, so instead we will use the following definition:
p
is your program, which takes in a decimal expansion of a number (list of integers), and returns 0 (≈Rational) or 1 (≈Irrational). If x
is an irrational number, then \$\limsup\limits_{n\rightarrow\infty}p(x[:n])=1\$, and if x
is rational the limit superior has to be 0. \$x[:n]\$ means the truncated decimal expansion, which is just the first n
digits after the decimal point.
Or in other words, iff x
is rational, there should be some n
such that all prefixes longer than n
return Rational
.
This is code-golf, so shortest code wins. Also, standard decision-problem rules apply, so instead of outputting 0 or 1, you can output true,false, "Irrational", "Rational", etc. As we know, \$0.999...=1\$, so you can assume that the decimal expansion doesn't contain a trail of repeating 9s. Instead of decimal, you can use binary. The input list is non-empty.
Explanation
Your program will receive a list/string of decimal digits corresponding to a truncated decimal expansion of that real number. For example, if we had the real number 0.31415926...
, the program could receive [3,1,4]
or [3]
or [3,1,4,1,5,9,2]
etc. as input. The program then has to guess whether the real number is irrational or not.
Of course, it's impossible to always guess correctly. What matters is what happens when the input length increases (the limiting behavior). Let's use the rational number 8/13=0.6153846153846153846153846...
as example. If we gave the program the first three digits [6,1,5]
, it might (incorrectly) guess that the number is irrational. But if we gave the program at least 30 digits, it might correctly guess (every time) that the number is rational.
In other words, if your program is given the decimal expansion of a rational number \$q\$, there must be some \$n\$ so that if at least \$n\$ digits are given to your program, it will always return Rational
.
For irrational numbers the opposite is true. For every irrational number \$r\$ there must be infinitely many \$n\$ so that if you give the first \$n\$ digits of \$r\$ as input, the program returns Irrational
.
Implementation hints
Every rational number has a repeating decimal expansion and all irrational numbers have non-repeating decimal expansions.
Possible program execution ("test cases")
# 41/333 = 0.123123123123...
[1] -> Rational
[1,2] -> Rational
[1,2,3] -> Irrational
[1,2,3,1] -> Irrational
[1,2,3,1,2] -> Irrational
[1,2,3,1,2,3] -> Rational
[1,2,3,1,2,3,1] -> Rational
[1,2,3,1,2,3,1,2] -> Rational
[1,2,3,1,2,3,1,2,3] -> Rational
[1,2,3,1,2,3,1,2,3,1] -> Rational
[1,2,3,1,2,3,1,2,3,1,2] -> Rational
[1,2,3,1,2,3,1,2,3,1,2,3] -> Rational
[1,2,3,1,2,3,1,2,3,1,2,3,1] -> Rational
# pi/10 = 0.31415926...
[3] -> Rational
[3,1] -> Rational
[3,1,4] -> Irrational
[3,1,4,1] -> Irrational
[3,1,4,1,5] -> Irrational
[3,1,4,1,5,9] -> Irrational
[3,1,4,1,5,9,2] -> Irrational
[3,1,4,1,5,9,2,6] -> Irrational
# 7/24 = 0.291666666...
[2] -> Rational
[2,9] -> Rational
[2,9,1] -> Irrational
[2,9,1,6] -> Irrational
[2,9,1,6,6] -> Irrational
[2,9,1,6,6,6] -> Rational
[2,9,1,6,6,6,6] -> Rational
[2,9,1,6,6,6,6,6] -> Rational
[2,9,1,6,6,6,6,6,6] -> Rational
[2,9,1,6,6,6,6,6,6,6] -> Rational
[2,9,1,6,6,6,6,6,6,6,6] -> Rational
# 0.10110000111111110...
[1] -> Rational
[1,0] -> Rational
[1,0,1] -> Irrational
[1,0,1,1] -> Rational
[1,0,1,1,0] -> Irrational
[1,0,1,1,0,0] -> Irrational
[1,0,1,1,0,0,0] -> Irrational
[1,0,1,1,0,0,0,0] -> Rational
[1,0,1,1,0,0,0,0,1] -> Irrational
[1,0,1,1,0,0,0,0,1,1] -> Irrational
[1,0,1,1,0,0,0,0,1,1,1] -> Irrational
[1,0,1,1,0,0,0,0,1,1,1,1] -> Irrational
[1,0,1,1,0,0,0,0,1,1,1,1,1] -> Irrational
[1,0,1,1,0,0,0,0,1,1,1,1,1,1] -> Irrational
[1,0,1,1,0,0,0,0,1,1,1,1,1,1,1] -> Irrational
[1,0,1,1,0,0,0,0,1,1,1,1,1,1,1,1] -> Rational
[1,0,1,1,0,0,0,0,1,1,1,1,1,1,1,1,0] -> Irrational
# 333/500 = 0.66600000000...
[6] -> Rational
[6,6] -> Rational
[6,6,6] -> Rational
[6,6,6,0] -> Irrational
[6,6,6,0,0] -> Irrational
[6,6,6,0,0,0] -> Rational
[6,6,6,0,0,0,0] -> Rational
[6,6,6,0,0,0,0,0] -> Rational
lim sup ... = 1
part as some math result you were stating (especially since I was unfamiliar with the notation) and was really confused. \$\endgroup\$x
is irrational iff \$\sum_{i=1}^{\infty}p(x[:i])=\infty\$ is easier parsed \$\endgroup\$x
is rational, there should be somen
such that all prefixes longer thann
returnRational
". The notation just gets in the way of a pretty straight forward claim. \$\endgroup\$