Problem 4 » 履歴 » バージョン 4
Noppi, 2023/12/27 13:03
| 1 | 1 | Noppi | [ホーム](https://redmine.noppi.jp) - [[Wiki|Project Euler]] |
|---|---|---|---|
| 2 | # [[Problem 4]] |
||
| 3 | |||
| 4 | 4 | Noppi | ## Largest Palindrome Product |
| 5 | A palindromic number reads the same both ways. The largest palindrome made from the product of two $2$-digit numbers is $9009 = 91 \times 99$. |
||
| 6 | Find the largest palindrome made from the product of two $3$-digit numbers. |
||
| 7 | |||
| 8 | ## 最大の回文積 |
||
| 9 | 左右どちらから読んでも同じ値になる数を回文数という. 2桁の数の積で表される回文数のうち, 最大のものは 9009 = 91 × 99 である. |
||
| 10 | では, 3桁の数の積で表される回文数の最大値を求めよ. |
||
| 11 | |||
| 12 | 1 | Noppi | ```scheme |
| 13 | #!r6rs |
||
| 14 | #!chezscheme |
||
| 15 | |||
| 16 | (import (chezscheme)) |
||
| 17 | |||
| 18 | (define (RotationNumber? num) |
||
| 19 | 3 | Noppi | (let* ([num-string (number->string num)] |
| 20 | [str-length (string-length num-string)] |
||
| 21 | [count (div str-length 2)]) |
||
| 22 | (let loop ([index 0]) |
||
| 23 | 1 | Noppi | (cond |
| 24 | [(<= count index) #t] |
||
| 25 | 3 | Noppi | [(equal? |
| 26 | 1 | Noppi | (string-ref num-string index) |
| 27 | (string-ref num-string (- str-length 1 index))) |
||
| 28 | 3 | Noppi | (loop (add1 index))] |
| 29 | 1 | Noppi | [else #f])))) |
| 30 | |||
| 31 | 4 | Noppi | (define (iota-3-digits) |
| 32 | (filter |
||
| 33 | (lambda (n) (<= 100 n)) |
||
| 34 | (iota 1000))) |
||
| 35 | |||
| 36 | (define (make-product-list) |
||
| 37 | (let ([i3d (iota-3-digits)]) |
||
| 38 | (fold-left |
||
| 39 | (lambda (lis current) |
||
| 40 | (let loop ([result lis] |
||
| 41 | [temp (map |
||
| 42 | (lambda (n) (* n current)) |
||
| 43 | i3d)]) |
||
| 44 | (if (null? temp) |
||
| 45 | result |
||
| 46 | (loop (cons (car temp) result) (cdr temp))))) |
||
| 47 | '() |
||
| 48 | i3d))) |
||
| 49 | |||
| 50 | (define (make-rotation-number-list) |
||
| 51 | (filter RotationNumber? (make-product-list))) |
||
| 52 | |||
| 53 | 1 | Noppi | (define answer-4 |
| 54 | 4 | Noppi | (apply max (make-rotation-number-list))) |
| 55 | 1 | Noppi | |
| 56 | (printf "4: ~D~%" answer-4) |
||
| 57 | ``` |