Problem 55 » 履歴 » バージョン 2
Noppi, 2024/01/22 08:20
| 1 | 1 | Noppi | [ホーム](https://redmine.noppi.jp) - [[Wiki|Project Euler]] |
|---|---|---|---|
| 2 | # [[Problem 55]] |
||
| 3 | |||
| 4 | ## Lychrel Numbers |
||
| 5 | If we take $47$, reverse and add, $47 + 74 = 121$, which is palindromic. |
||
| 6 | |||
| 7 | <p>Not all numbers produce palindromes so quickly. For example,</p> |
||
| 8 | \begin{align} |
||
| 9 | 349 + 943 &= 1292\\ |
||
| 10 | 1292 + 2921 &= 4213\\ |
||
| 11 | 4213 + 3124 &= 7337 |
||
| 12 | \end{align} |
||
| 13 | |||
| 14 | That is, $349$ took three iterations to arrive at a palindrome. |
||
| 15 | |||
| 16 | Although no one has proved it yet, it is thought that some numbers, like $196$, never produce a palindrome. A number that never forms a palindrome through the reverse and add process is called a Lychrel number. Due to the theoretical nature of these numbers, and for the purpose of this problem, we shall assume that a number is Lychrel until proven otherwise. In addition you are given that for every number below ten-thousand, it will either (i) become a palindrome in less than fifty iterations, or, (ii) no one, with all the computing power that exists, has managed so far to map it to a palindrome. In fact, $10677$ is the first number to be shown to require over fifty iterations before producing a palindrome: $4668731596684224866951378664$ ($53$ iterations, $28$-digits). |
||
| 17 | |||
| 18 | Surprisingly, there are palindromic numbers that are themselves Lychrel numbers; the first example is $4994$. |
||
| 19 | |||
| 20 | How many Lychrel numbers are there below ten-thousand? |
||
| 21 | |||
| 22 | NOTE: Wording was modified slightly on 24 April 2007 to emphasise the theoretical nature of Lychrel numbers. |
||
| 23 | |||
| 24 | ## Lychrel数 |
||
| 25 | 47とその反転を足し合わせると, 47 + 74 = 121となり, 回文数になる. |
||
| 26 | |||
| 27 | 全ての数が素早く回文数になるわけではない. 349を考えよう, |
||
| 28 | |||
| 29 | 1. 349 + 943 = 1292, |
||
| 30 | 1. 1292 + 2921 = 4213 |
||
| 31 | 1. 4213 + 3124 = 7337 |
||
| 32 | |||
| 33 | 349は, 3回の操作を経て回文数になる. |
||
| 34 | |||
| 35 | まだ証明はされていないが, 196のようないくつかの数字は回文数にならないと考えられている. 反転したものを足すという操作を経ても回文数にならないものをLychrel数と呼ぶ. 先のような数の理論的な性質により, またこの問題の目的のために, Lychrel数で無いと証明されていない数はLychrel数だと仮定する. |
||
| 36 | |||
| 37 | 更に, 10000未満の数については,常に以下のどちらか一方が成り立つと仮定してよい. |
||
| 38 | |||
| 39 | 1. 50回未満の操作で回文数になる |
||
| 40 | 1. まだ誰も回文数まで到達していない |
||
| 41 | |||
| 42 | 実際, 10677が50回以上の操作を必要とする最初の数である: 4668731596684224866951378664 (53回の操作で28桁のこの回文数になる). |
||
| 43 | |||
| 44 | 驚くべきことに, 回文数かつLychrel数であるものが存在する. 最初の数は4994である. |
||
| 45 | |||
| 46 | 10000未満のLychrel数の個数を答えよ. |
||
| 47 | |||
| 48 | 注: 2007/04/24にLychrel数の理論的な性質を強調するために文面が修正された. |
||
| 49 | |||
| 50 | ```scheme |
||
| 51 | 2 | Noppi | (import (scheme base) |
| 52 | (gauche base)) |
||
| 53 | |||
| 54 | (define (rotation-number num) |
||
| 55 | (assume (exact-integer? num)) |
||
| 56 | (assume (not (negative? num))) |
||
| 57 | (if (zero? num) |
||
| 58 | 0 |
||
| 59 | (let loop ([rest num] [result 0]) |
||
| 60 | (if (zero? rest) |
||
| 61 | result |
||
| 62 | (loop (div rest 10) |
||
| 63 | (+ (* result 10) |
||
| 64 | (mod rest 10))))))) |
||
| 65 | |||
| 66 | (define (palindrome? num) |
||
| 67 | (assume (exact-integer? num)) |
||
| 68 | (assume (not (negative? num))) |
||
| 69 | (= num |
||
| 70 | (rotation-number num))) |
||
| 71 | |||
| 72 | (define (lychrel? num) |
||
| 73 | (assume (exact-integer? num)) |
||
| 74 | (assume (not (negative? num))) |
||
| 75 | (let loop ([index 0] [current num]) |
||
| 76 | (if (<= 50 index) |
||
| 77 | #t |
||
| 78 | (let ([next (+ current |
||
| 79 | (rotation-number current))]) |
||
| 80 | (if (palindrome? next) |
||
| 81 | #f |
||
| 82 | (loop (+ index 1) next)))))) |
||
| 83 | |||
| 84 | (define answer-55 |
||
| 85 | (length |
||
| 86 | (filter lychrel? |
||
| 87 | (iota 10000)))) |
||
| 88 | |||
| 89 | (format #t "55: ~d~%" answer-55) |
||
| 90 | 1 | Noppi | ``` |