From fbd206306bf9d1081b077925b7c4c5aae4841114 Mon Sep 17 00:00:00 2001 From: daviddoji Date: Mon, 11 Oct 2021 16:36:21 +0200 Subject: [PATCH] Solution to problem 57 in julia --- src/Julia/Problem057.jl | 50 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/Julia/Problem057.jl diff --git a/src/Julia/Problem057.jl b/src/Julia/Problem057.jl new file mode 100644 index 0000000..be51ce1 --- /dev/null +++ b/src/Julia/Problem057.jl @@ -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())