aboutsummaryrefslogtreecommitdiff
path: root/julia
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2021-06-24 18:06:04 +0200
committerCharles Cabergs <me@cacharle.xyz>2021-07-26 17:06:41 +0200
commit474d03cbb64fa822e2ab4f360ff699d98c666282 (patch)
tree3a2be3e59ba777e985852fdac402674591ef43c2 /julia
parentd27319e533ab17da0918ef1400b2a661a644d55e (diff)
downloadproject_euler-474d03cbb64fa822e2ab4f360ff699d98c666282.tar.gz
project_euler-474d03cbb64fa822e2ab4f360ff699d98c666282.tar.bz2
project_euler-474d03cbb64fa822e2ab4f360ff699d98c666282.zip
problem 65 in julia
Diffstat (limited to 'julia')
-rw-r--r--julia/065-convergents_of_e.jl59
1 files changed, 59 insertions, 0 deletions
diff --git a/julia/065-convergents_of_e.jl b/julia/065-convergents_of_e.jl
new file mode 100644
index 0000000..5f43d6a
--- /dev/null
+++ b/julia/065-convergents_of_e.jl
@@ -0,0 +1,59 @@
+###
+# Convergents of e
+# Problem 65
+#
+# The square root of 2 can be written as an infinite continued fraction.
+# $\sqrt{2} = 1 + \dfrac{1}{2 + \dfrac{1}{2 + \dfrac{1}{2 + \dfrac{1}{2 + ...}}}}$
+# The infinite continued fraction can be written, $\sqrt{2} = [1; (2)]$, $(2)$ indicates
+# that 2 repeats ad infinitum. In a similar way, $\sqrt{23} = [4; (1, 3, 1, 8)]$.
+# It turns out that the sequence of partial values of continued fractions for square roots
+# provide the best rational approximations. Let us consider the convergents for $\sqrt{2}$.
+# $
+# 1 + \dfrac{1}{2} = \dfrac{3}{2}\\
+# 1 + \dfrac{1}{2 + \dfrac{1}{2}} = \dfrac{7}{5}\\
+# 1 + \dfrac{1}{2 + \dfrac{1}{2 + \dfrac{1}{2}}} = \dfrac{17}{12}\\
+# 1 + \dfrac{1}{2 + \dfrac{1}{2 + \dfrac{1}{2 + \dfrac{1}{2}}}} = \dfrac{41}{29}
+# $
+# Hence the sequence of the first ten convergents for $\sqrt{2}$ are:
+# $1, \dfrac{3}{2}, \dfrac{7}{5}, \dfrac{17}{12}, \dfrac{41}{29}, \dfrac{99}{70},
+# \dfrac{239}{169}, \dfrac{577}{408}, \dfrac{1393}{985}, \dfrac{3363}{2378}, ...$
+# What is most surprising is that the important mathematical constant,$e = [2; 1, 2, 1, 1,
+# 4, 1, 1, 6, 1, ... , 1, 2k, 1, ...]$.
+# The first ten terms in the sequence of convergents for e are:
+# $2, 3, \dfrac{8}{3}, \dfrac{11}{4}, \dfrac{19}{7}, \dfrac{87}{32}, \dfrac{106}{39},
+# \dfrac{193}{71}, \dfrac{1264}{465}, \dfrac{1457}{536}, ...$
+# The sum of digits in the numerator of the 10th convergent is $1 + 4 + 5 + 7 = 17$.
+# Find the sum of digits in the numerator of the 100th convergent of the continued fraction
+# for $e$.
+###
+
+
+# blackpenredpen continuous fractions: https://www.youtube.com/watch?v=vw4eYXMWYEw
+#
+# the notation for continuous fraction is [start, (sequence)]:
+#
+# 1
+# start + ---------------
+# 1
+# sequence[1] + ----------------
+# sequence[2] + ...
+
+using Base.Iterators
+
+function converge(start, sequence, len)
+ function rec(sequence, len)::Rational{BigInt}
+ len == 1 && return 0
+ return 1 // (sequence[1] + rec(circshift(sequence, -1), len - 1))
+ end
+ return start + rec(sequence, len)
+end
+
+result = (
+ collect(flatten((1, 2k, 1) for k in 1:1000)) |>
+ x ->converge(2, x, 100) |>
+ numerator |>
+ digits |>
+ sum
+)
+
+println(result)