プロジェクト

全般

プロフィール

Problem 65 » 履歴 » バージョン 3

Noppi, 2024/01/29 05:53

1 1 Noppi
[ホーム](https://redmine.noppi.jp) - [[Wiki|Project Euler]]
2
# [[Problem 65]]
3
4
## Convergents of $e$
5
The square root of $2$ can be written as an infinite continued fraction.
6
7
$\sqrt{2} = 1 + \dfrac{1}{2 + \dfrac{1}{2 + \dfrac{1}{2 + \dfrac{1}{2 + ...}}}}$
8
9
The infinite continued fraction can be written, $\sqrt{2} = [1; (2)]$, $(2)$ indicates that $2$ repeats <i>ad infinitum</i>. In a similar way, $\sqrt{23} = [4; (1, 3, 1, 8)]$.
10
11
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}$.
12
13
<p>$\begin{align}
14
&amp;1 + \dfrac{1}{2} = \dfrac{3}{2} \\
15
&amp;1 + \dfrac{1}{2 + \dfrac{1}{2}} = \dfrac{7}{5}\\
16
&amp;1 + \dfrac{1}{2 + \dfrac{1}{2 + \dfrac{1}{2}}} = \dfrac{17}{12}\\
17
&amp;1 + \dfrac{1}{2 + \dfrac{1}{2 + \dfrac{1}{2 + \dfrac{1}{2}}}} = \dfrac{41}{29}
18
\end{align}$</p>
19
20
Hence the sequence of the first ten convergents for $\sqrt{2}$ are:
21
22
$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}, ...$
23
24
What is most surprising is that the important mathematical constant,
25
$e = [2; 1, 2, 1, 1, 4, 1, 1, 6, 1, ... , 1, 2k, 1, ...]$.
26
27
The first ten terms in the sequence of convergents for $e$ are:
28
29
$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}, ...$
30
31
The sum of digits in the numerator of the $10$<sup>th</sup> convergent is $1 + 4 + 5 + 7 = 17$.
32
33
Find the sum of digits in the numerator of the $100$<sup>th</sup> convergent of the continued fraction for $e$.
34
35
## $e$ の近似分数
36
2の平方根は無限連分数として書くことができる.
37
38
√2 = 1 + 1/(2 + 1/(2 + 1/(2 + 1/(2 + ...))))
39
40
無限連分数である √2 = [1;(2)] と書くことができるが, (2) は2が無限に繰り返されることを示す. 同様に, √23 = [4;(1,3,1,8)].
41
42
平方根の部分的な連分数の数列から良い有理近似が得られることが分かる.√2の近似分数について考えよう.
43
44
1 + 1/2 = 3/2
45
1 + 1/(2 + 1/2) = 7/5
46
1 + 1/(2 + 1/(2 + 1/2)) = 17/12
47
1 + 1/(2 + 1/(2 + 1/(2 + 1/2))) = 41/29
48
49
従って, √2の近似分数からなる数列の最初の10項は:
50
51
1, 3/2, 7/5, 17/12, 41/29, 99/70, 239/169, 577/408, 1393/985, 3363/2378, ...
52
53
もっとも驚くべきことに, 数学的に重要な定数,
54
55
e = [2; 1,2,1, 1,4,1, 1,6,1 , ... , 1,2k,1, ...].
56
57
e の近似分数からなる数列の最初の10項は:
58
59
2, 3, 8/3, 11/4, 19/7, 87/32, 106/39, 193/71, 1264/465, 1457/536, ...
60
61
10項目の近似分数の分子の桁を合計すると1+4+5+7=17である.
62
63
e についての連分数である近似分数の100項目の分子の桁の合計を求めよ.
64
65
```scheme
66 2 Noppi
(import (scheme base)
67
        (gauche base))
68
69
(define (continued-fraction lis)
70
  (let loop ([rest (reverse lis)]
71
             [result 0])
72
    (cond
73
      [(null? rest) result]
74
      [(zero? result)
75
       (loop (cdr rest) (car rest))]
76
      [else
77
        (loop (cdr rest)
78
              (+ (car rest)
79
                 (/ 1 result)))])))
80
81 3 Noppi
(define (e-generator)
82 2 Noppi
  (let ([index 0] [current-center 2])
83
    (^[]
84
      (let ([next-index (mod (+ index 1) 3)])
85
        (ecase index
86
          [(0 2)
87
           (set! index next-index)
88
           1]
89
          [(1)
90 3 Noppi
           (begin0
91
             current-center
92
             (set! index next-index)
93
             (set! current-center (+ current-center 2)))])))))
94 2 Noppi
95 3 Noppi
(define (e-list)
96
  (generator->lseq 2 (e-generator)))
97 2 Noppi
98
(define (number->list num)
99
  (assume (exact-integer? num))
100
  (assume (not (negative? num)))
101
  (if (zero? num)
102
    '(0)
103
    (let loop ([rest num] [current '()])
104
      (if (zero? rest)
105
        current
106
        (loop (div rest 10)
107
              (cons (mod rest 10)
108
                    current))))))
109
110
(define answer-65
111
  (apply +
112
         (number->list
113
           (numerator
114
             (continued-fraction
115 3 Noppi
               (take (e-list) 100))))))
116 2 Noppi
117
(format #t "65: ~d~%" answer-65)
118 1 Noppi
```