Problem 57 » 履歴 » バージョン 2
Noppi, 2024/01/22 11:49
1 | 1 | Noppi | [ホーム](https://redmine.noppi.jp) - [[Wiki|Project Euler]] |
---|---|---|---|
2 | # [[Problem 57]] |
||
3 | |||
4 | ## Square Root Convergents |
||
5 | It is possible to show that the square root of two can be expressed as an infinite continued fraction. |
||
6 | |||
7 | <p style="text-align:center">$\sqrt 2 =1+ \frac 1 {2+ \frac 1 {2 +\frac 1 {2+ \dots}}}$</p> |
||
8 | |||
9 | By expanding this for the first four iterations, we get: |
||
10 | |||
11 | $1 + \frac 1 2 = \frac 32 = 1.5$ |
||
12 | $1 + \frac 1 {2 + \frac 1 2} = \frac 7 5 = 1.4$ |
||
13 | $1 + \frac 1 {2 + \frac 1 {2+\frac 1 2}} = \frac {17}{12} = 1.41666 \dots$ |
||
14 | $1 + \frac 1 {2 + \frac 1 {2+\frac 1 {2+\frac 1 2}}} = \frac {41}{29} = 1.41379 \dots$ |
||
15 | |||
16 | The next three expansions are $\frac {99}{70}$, $\frac {239}{169}$, and $\frac {577}{408}$, but the eighth expansion, $\frac {1393}{985}$, is the first example where the number of digits in the numerator exceeds the number of digits in the denominator. |
||
17 | |||
18 | In the first one-thousand expansions, how many fractions contain a numerator with more digits than the denominator? |
||
19 | |||
20 | ## 平方根の近似分数 |
||
21 | 2の平方根は無限に続く連分数で表すことができる. |
||
22 | |||
23 | <div style="text-align:center">√ 2 = 1 + 1/(2 + 1/(2 + 1/(2 + ... ))) = 1.414213...</div> |
||
24 | |||
25 | 最初の4回の繰り返しを展開すると以下が得られる. |
||
26 | |||
27 | 1 + 1/2 = 3/2 = 1.5 |
||
28 | 1 + 1/(2 + 1/2) = 7/5 = 1.4 |
||
29 | 1 + 1/(2 + 1/(2 + 1/2)) = 17/12 = 1.41666... |
||
30 | 1 + 1/(2 + 1/(2 + 1/(2 + 1/2))) = 41/29 = 1.41379... |
||
31 | |||
32 | 次の3つの項は99/70, 239/169, 577/408である. 第8項は1393/985である. これは分子の桁数が分母の桁数を超える最初の例である. |
||
33 | |||
34 | 最初の1000項を考えたとき, 分子の桁数が分母の桁数を超える項はいくつあるか? |
||
35 | |||
36 | ```scheme |
||
37 | 2 | Noppi | ;;; |
38 | ;;; Gaucheコード(なぜか不正解になる…) |
||
39 | ;;; |
||
40 | (import (scheme base) |
||
41 | (gauche base) |
||
42 | (scheme inexact)) |
||
43 | |||
44 | (define (digit-num num) |
||
45 | (assume (exact-integer? num)) |
||
46 | (assume (not (negative? num))) |
||
47 | (if (zero? num) |
||
48 | 1 |
||
49 | (+ (floor->exact (log num 10)) |
||
50 | 1))) |
||
51 | |||
52 | (define continued-fraction |
||
53 | (let loop ([index 1] [current 1] [result '()]) |
||
54 | (if (< 1000 index) |
||
55 | (reverse result) |
||
56 | (let ([next (+ 1 |
||
57 | (/ 1 |
||
58 | (+ current 1)))]) |
||
59 | (loop (+ index 1) |
||
60 | next |
||
61 | (cons next result)))))) |
||
62 | |||
63 | (define target-fraction |
||
64 | (filter (^r |
||
65 | (< (digit-num (denominator r)) |
||
66 | (digit-num (numerator r)))) |
||
67 | continued-fraction)) |
||
68 | |||
69 | (define answer-57 |
||
70 | (length target-fraction)) |
||
71 | |||
72 | (format #t "57: ~d~%" answer-57) |
||
73 | ``` |
||
74 | |||
75 | ```scheme |
||
76 | ;;; |
||
77 | ;;; Chez Schemeコード(正解) |
||
78 | ;;; |
||
79 | #!r6rs |
||
80 | #!chezscheme |
||
81 | |||
82 | (import (chezscheme)) |
||
83 | |||
84 | (define (digit-num num) |
||
85 | (assert (exact? num)) |
||
86 | (assert (integer? num)) |
||
87 | (assert (not (negative? num))) |
||
88 | (if (zero? num) |
||
89 | 1 |
||
90 | (+ (exact (floor (log num 10))) |
||
91 | 1))) |
||
92 | |||
93 | (define continued-fraction |
||
94 | (let loop ([index 1] [current 1] [result '()]) |
||
95 | (if (< 1000 index) |
||
96 | (reverse result) |
||
97 | (let ([next (+ 1 |
||
98 | (/ 1 |
||
99 | (+ current 1)))]) |
||
100 | (loop (+ index 1) |
||
101 | next |
||
102 | (cons next result)))))) |
||
103 | |||
104 | (define target-fraction |
||
105 | (filter (lambda (r) |
||
106 | (< (digit-num (denominator r)) |
||
107 | (digit-num (numerator r)))) |
||
108 | continued-fraction)) |
||
109 | |||
110 | (define answer-57 |
||
111 | (length target-fraction)) |
||
112 | |||
113 | (printf "57: ~d~%" answer-57) |
||
114 | 1 | Noppi | ``` |