Problem 66 » 履歴 » バージョン 3
Noppi, 2024/01/29 06:09
| 1 | 1 | Noppi | [ホーム](https://redmine.noppi.jp) - [[Wiki|Project Euler]] |
|---|---|---|---|
| 2 | # [[Problem 66]] |
||
| 3 | |||
| 4 | ## Diophantine Equation |
||
| 5 | Consider quadratic Diophantine equations of the form: |
||
| 6 | |||
| 7 | $$x^2 - Dy^2 = 1$$ |
||
| 8 | |||
| 9 | For example, when $D=13$, the minimal solution in $x$ is $649^2 - 13 \times 180^2 = 1$. |
||
| 10 | |||
| 11 | It can be assumed that there are no solutions in positive integers when $D$ is square. |
||
| 12 | |||
| 13 | <p>By finding minimal solutions in $x$ for $D = \{2, 3, 5, 6, 7\}$, we obtain the following:</p> |
||
| 14 | \begin{align} |
||
| 15 | 3^2 - 2 \times 2^2 &= 1\\ |
||
| 16 | 2^2 - 3 \times 1^2 &= 1\\ |
||
| 17 | {\color{red}{\mathbf 9}}^2 - 5 \times 4^2 &= 1\\ |
||
| 18 | 5^2 - 6 \times 2^2 &= 1\\ |
||
| 19 | 8^2 - 7 \times 3^2 &= 1 |
||
| 20 | \end{align} |
||
| 21 | |||
| 22 | Hence, by considering minimal solutions in $x$ for $D \le 7$, the largest $x$ is obtained when $D=5$. |
||
| 23 | |||
| 24 | Find the value of $D \le 1000$ in minimal solutions of $x$ for which the largest value of $x$ is obtained. |
||
| 25 | |||
| 26 | ## ディオファントス方程式 |
||
| 27 | 次の形式の, 2次のディオファントス方程式を考えよう: |
||
| 28 | |||
| 29 | $$x^2 - Dy^2 = 1$$ |
||
| 30 | |||
| 31 | たとえば D=13 のとき, x を最小にする解は 6492 - 131802 = 1 である. |
||
| 32 | |||
| 33 | D が平方数(square)のとき, 正整数のなかに解は存在しないと考えられる. |
||
| 34 | |||
| 35 | <p>D = {2, 3, 5, 6, 7} に対して x を最小にする解は次のようになる: |
||
| 36 | \begin{align} |
||
| 37 | 3^2 - 2 \times 2^2 &= 1\\ |
||
| 38 | 2^2 - 3 \times 1^2 &= 1\\ |
||
| 39 | {\color{red}{\mathbf 9}}^2 - 5 \times 4^2 &= 1\\ |
||
| 40 | 5^2 - 6 \times 2^2 &= 1\\ |
||
| 41 | 8^2 - 7 \times 3^2 &= 1 |
||
| 42 | \end{align} |
||
| 43 | </p> |
||
| 44 | |||
| 45 | したがって, D ≤ 7 に対して x を最小にする解を考えると, D=5 のとき x は最大である. |
||
| 46 | |||
| 47 | D ≤ 1000 に対する x を最小にする解で, x が最大になるような D の値を見つけよ. |
||
| 48 | |||
| 49 | ```scheme |
||
| 50 | 2 | Noppi | ;;; 素直なアルゴリズムで書いたら止まらなくなったので |
| 51 | ;;; このやり方は使えない |
||
| 52 | |||
| 53 | (import (scheme base) |
||
| 54 | (gauche base)) |
||
| 55 | |||
| 56 | (define (d-list) |
||
| 57 | (filter (^n |
||
| 58 | (let-values ([(_ b) (exact-integer-sqrt n)]) |
||
| 59 | (not (zero? b)))) |
||
| 60 | (iota 999 2))) |
||
| 61 | |||
| 62 | (define (diophantine d) |
||
| 63 | (let loop ([y 1]) |
||
| 64 | (let ([dy2 (* d (square y))]) |
||
| 65 | (let-values ([(x-1 dif) (exact-integer-sqrt dy2)]) |
||
| 66 | (if (zero? dif) |
||
| 67 | (loop (+ y 1)) |
||
| 68 | (let ([x (+ x-1 1)]) |
||
| 69 | (if (= (- (square x) |
||
| 70 | dy2) |
||
| 71 | 1) |
||
| 72 | 3 | Noppi | `(,x ,y . ,d) |
| 73 | 2 | Noppi | (loop (+ y 1))))))))) |
| 74 | |||
| 75 | ;;; D = 109 の時に死んだので連分数展開を使用した方法に切り替える必要がある |
||
| 76 | ;;; ちなみに、D = 109 の時の x と y の値は次の通りらしい… |
||
| 77 | ;;; x = 158070671986249, y = 15140424455100 |
||
| 78 | ;;; (see https://ja.wikipedia.org/wiki/%E3%83%9A%E3%83%AB%E6%96%B9%E7%A8%8B%E5%BC%8F) |
||
| 79 | (define (diophantine-1000) |
||
| 80 | (map (cut diophantine <>) |
||
| 81 | (d-list))) |
||
| 82 | |||
| 83 | (define answer-66) |
||
| 84 | |||
| 85 | (format #t "66: ~d~%" answer-66) |
||
| 86 | 1 | Noppi | ``` |