Solution to problem 57 in julia

This commit is contained in:
David Doblas Jiménez 2021-10-11 16:36:21 +02:00
parent 4fdf019a4e
commit fbd206306b

50
src/Julia/Problem057.jl Normal file
View File

@ -0,0 +1,50 @@
#=
Created on 11 Oct 2021
@author: David Doblas Jiménez
@email: daviddoji@pm.me
Solution for Problem 57 of Project Euler
https://projecteuler.net/problem=57
=#
using BenchmarkTools
function Problem57()
#=
It is possible to show that the square root of two can be expressed
as an infinite continued fraction.
By expanding this for the first four iterations, we get:
1 + 1/2 = 3/2 = 1.5
1 + 1/2+1/2 = 7/5 = 1.4
1 + 1/2+1/2+1/2 = 17/12 = 1.41666...
1 + 1/2+1/2+1/2+1/2 = 41/29 = 1.41379...
The next three expansions are 99/70, 239/169, and 577/408, but the eighth
expansion, 1393/985, is the first example where the number of digits in
the numerator exceeds the number of digits in the denominator.
In the first one-thousand expansions, how many fractions contain a numerator
with more digits than the denominator
=#
ans = 0
f = 1 // 2
for i in 1:1000
f = big(1) / (2 + f)
result = 1 + f
if length(digits(numerator(result))) > length(digits(denominator(result)))
ans += 1
end
end
return ans
end
println("Time to evaluate Problem 57:")
@btime Problem57()
println("")
println("Result for Problem 57: ", Problem57())