Problem 57¶
Square Root Convergents¶
It is possible to show that the square root of two can be expressed as an infinite continued fraction.
$\sqrt 2 =1+ \frac 1 {2+ \frac 1 {2 +\frac 1 {2+ \dots}}}$
By expanding this for the first four iterations, we get:
$1 + \frac 1 2 = \frac 32 = 1.5$
$1 + \frac 1 {2 + \frac 1 2} = \frac 7 5 = 1.4$
$1 + \frac 1 {2 + \frac 1 {2+\frac 1 2}} = \frac {17}{12} = 1.41666 \dots$
$1 + \frac 1 {2 + \frac 1 {2+\frac 1 {2+\frac 1 2}}} = \frac {41}{29} = 1.41379 \dots$
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.
In the first one-thousand expansions, how many fractions contain a numerator with more digits than the denominator?
平方根の近似分数¶
2の平方根は無限に続く連分数で表すことができる.
最初の4回の繰り返しを展開すると以下が得られる.
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...
次の3つの項は99/70, 239/169, 577/408である. 第8項は1393/985である. これは分子の桁数が分母の桁数を超える最初の例である.
最初の1000項を考えたとき, 分子の桁数が分母の桁数を超える項はいくつあるか?
;;;
;;; Gaucheコード(0.9.14_pre1で修正されて正しい答になることを確認)
;;;
(import (scheme base)
(gauche base)
(scheme inexact))
(define (digit-num num)
(assume (exact-integer? num))
(assume (not (negative? num)))
(if (zero? num)
1
(+ (floor->exact (log num 10))
1)))
(define continued-fraction
(let loop ([index 1] [current 1] [result '()])
(if (< 1000 index)
(reverse result)
(let ([next (+ 1
(/ 1
(+ current 1)))])
(loop (+ index 1)
next
(cons next result))))))
(define target-fraction
(filter (^r
(< (digit-num (denominator r))
(digit-num (numerator r))))
continued-fraction))
(define answer-57
(length target-fraction))
(format #t "57: ~d~%" answer-57)
;;;
;;; Chez Schemeコード(正解)
;;;
#!r6rs
#!chezscheme
(import (chezscheme))
(define (digit-num num)
(assert (exact? num))
(assert (integer? num))
(assert (not (negative? num)))
(if (zero? num)
1
(+ (exact (floor (log num 10)))
1)))
(define continued-fraction
(let loop ([index 1] [current 1] [result '()])
(if (< 1000 index)
(reverse result)
(let ([next (+ 1
(/ 1
(+ current 1)))])
(loop (+ index 1)
next
(cons next result))))))
(define target-fraction
(filter (lambda (r)
(< (digit-num (denominator r))
(digit-num (numerator r))))
continued-fraction))
(define answer-57
(length target-fraction))
(printf "57: ~d~%" answer-57)
Noppi が2024/01/23に更新 · 3件の履歴