I have the following code:
function recursion(i::BigFloat)
r = BigFloat(0)
if i >= 1.0
i = i - 1.0
r = 1.0/(2.0+recursion(i))
end
return r
end
function main()
solution = 0
i = BigFloat(1)
while i < 1000
sum = 1 + recursion(i)
print("\nsum: ",rationalize(sum))
if length(digits(numerator(rationalize(sum)))) > length(digits(denominator(rationalize(sum))))
solution = solution + 1
print(" <------- found")
end
i = i + 1.0
end
return solution
end
solution = main()
The goal is to find expansions of the sqrt(two) using the following equation:
(image taken from "Square Root of 2" wikipedia page)
where the rational representation has a numerator with a number of digits greater than the denominator's number of digits.
My code's logic seems to be working, however there is an upper limit where it seems to be breaking the rationalize() function when using a BigFloat type.
sum: 3//2
sum: 7//5
sum: 17//12
sum: 41//29
sum: 99//70
sum: 239//169
sum: 577//408
sum: 1393//985 <------- found
sum: 3363//2378
sum: 8119//5741
sum: 19601//13860
sum: 47321//33461
sum: 114243//80782 <------- found
sum: 275807//195025
sum: 665857//470832
sum: 1607521//1136689
sum: 3880899//2744210
sum: 9369319//6625109
sum: 22619537//15994428
sum: 54608393//38613965
sum: 131836323//93222358 <------- found
sum: 318281039//225058681
sum: 768398401//543339720
sum: 1855077841//1311738121
sum: 4478554083//3166815962
sum: 10812186007//7645370045 <------- found
sum: 26102926097//18457556052
sum: 63018038201//44560482149
sum: 152139002499//107578520350
sum: 367296043199//259717522849
sum: 886731088897//627013566048
sum: 2140758220993//1513744654945
sum: 5168247530883//3654502875938
sum: 12477253282759//8822750406821 <------- found
sum: 30122754096401//21300003689580
sum: 72722761475561//51422757785981
sum: 175568277047523//124145519261542
sum: 423859315570607//299713796309065
sum: 1023286908188737//723573111879672 <------- found
sum: 2470433131948081//1746860020068409
sum: 5964153172084899//4217293152016490
sum: 14398739476117879//10181446324101389
sum: 34761632124320657//24580185800219268
sum: 83922003724759193//59341817924539925
sum: 202605639573839043//143263821649299118
sum: 489133282872437279//345869461223138161
sum: 1180872205318713601//835002744095575440 <------- found
sum: 2850877693509864481//2015874949414289041
sum: 6882627592338442563//4866752642924153522
sum: 6882627592338442563//4866752642924153522
sum: 6882627592338442563//4866752642924153522
sum: 6882627592338442563//4866752642924153522
sum: 6882627592338442563//4866752642924153522
sum: 6882627592338442563//4866752642924153522
sum: 6882627592338442563//4866752642924153522
sum: 6882627592338442563//4866752642924153522
sum: 6882627592338442563//4866752642924153522
sum: 6882627592338442563//4866752642924153522
sum: 6882627592338442563//4866752642924153522
It gets "stuck" at the fraction 6882627592338442563//4866752642924153522. Presumably because there is some sort of upper limit within the rationalize function.
Why is it getting stuck here and how can I circumvent the problem?

There are a few issues here, all dealing with making sure that you're using types that can get as accurate or as large as you want.
First, you need to break the habit of putting decimals in your exact numbers — as in
1.0and2.0, which are interpreted as literalFloat64s. That's fine when you're only ever dealing with 64-bit floats, but you don't want to do algebra with aFloat64and aBigFloat. In this case, since your floats are exact integers, just use the integers1and2, and Julia will promote properly toBigFloat. For things that aren't exact integers, you can explicitly give literalRationals, and the compiler will convert them to the appropriate type efficiently. Or you can explicitly convert to the number type you need, as insqrt(big(2))— though usually it's better to detect the input type and then do something likesqrt(T(2)). Unlike in — say, C++ — if you enter1/2, you'll get 0.5 as aFloat64. On the other hand,1/(2+recursion(i))will first convert2+recursion(i)to the appropriateBigFloat, and then take the true inverse.Second, if you look at the docstring for
rationalize, you see that it has an optional argument of the type, which defaults toInt. This means the result must have numerator and denominator of this type. ButInt64can only represent numbers up totypemax(Int64), which is 9223372036854775807. And your "stuck" numerator is ~75% as large, so you probably just can't get much larger. Instead of using the default, just call it asrationalize(BigInt, sum), and you'll be able to get much larger numbers on top and bottom.Third, note the third argument of
rationalize, which says that it will stop trying to find a better approximation once the result is withintol, which defaults to the machine precision of the input. Forsqrt(big(2)), that's about 1.7e-77. Sorationalizewon't give you anything more once the result is within that distance ofsqrt(big(2)). So even withBig*, you're results will get "stuck" eventually.Anyway, putting that all together:
And the result looks like
So it does get "stuck", but just evaluate that final result compared to
sqrt(big(2)), and you should be pretty happy.