Problem 4 » 履歴 » リビジョン 4
リビジョン 3 (Noppi, 2023/12/27 04:45) → リビジョン 4/6 (Noppi, 2023/12/27 13:03)
[ホーム](https://redmine.noppi.jp) - [[Wiki|Project Euler]] # [[Problem 4]] ## Largest Palindrome Product 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$. Find the largest palindrome made from the product of two $3$-digit numbers. ## 最大の回文積 左右どちらから読んでも同じ値になる数を回文数という. 2桁の数の積で表される回文数のうち, 最大のものは 9009 = 91 × 99 である. では, 3桁の数の積で表される回文数の最大値を求めよ. ```scheme #!r6rs #!chezscheme (import (chezscheme)) (define (RotationNumber? num) (let* ([num-string (number->string num)] [str-length (string-length num-string)] [count (div str-length 2)]) (let loop ([index 0]) (cond [(<= count index) #t] [(equal? (string-ref num-string index) (string-ref num-string (- str-length 1 index))) (loop (add1 index))] [else #f])))) (define (iota-3-digits) answer-4 (filter (lambda (n) (<= 100 n)) (iota 1000))) (define (make-product-list) (let ([i3d (iota-3-digits)]) loop1 ([num1 100] [result 0]) (fold-left (if (< 999 num1) (lambda (lis current) result (let loop2 ([num2 100] [result result]) (if (< 999 num2) (loop1 (add1 num1) result) (let loop ([result lis] [temp (map (lambda (n) ([num (* n current)) i3d)]) num1 num2)]) (if (null? temp) (RotationNumber? num) (loop2 (add1 num2) (max result (loop (cons (car temp) result) (cdr temp))))) '() i3d))) (define (make-rotation-number-list) (filter RotationNumber? (make-product-list))) (define answer-4 (apply max (make-rotation-number-list))) num)) (loop2 (add1 num2) result)))))))) (printf "4: ~D~%" answer-4) ```