プロジェクト

全般

プロフィール

Problem 4 » 履歴 » リビジョン 5

リビジョン 4 (Noppi, 2023/12/27 13:03) → リビジョン 5/6 (Noppi, 2023/12/28 02:06)

[ホーム](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 (palindrome? (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] 
         [(char=? [(equal? 
            (string-ref num-string index) 
            (string-ref num-string (- str-length 1 index))) 
          (loop (add1 index))] 
         [else #f])))) 

 (define iota-3digits (iota-3-digits) 
   (filter 
     (lambda (n) (<= 100 n)) 
     (iota 1000))) 

 (define product-3digits-numbers (make-product-list) 
   (let ([i3d (iota-3-digits)]) 
     (fold-left 
     
       (lambda (lis current) 
       
         (let loop ([result lis] 
                  
                    [temp (map 
                          
                            (lambda (n) (* n current)) 
                          iota-3digits)]) 
         
                            i3d)]) 
           (if (null? temp) 
           
             result 
           
             (loop (cons (car temp) result) (cdr temp))))) 
     
       '() 
     iota-3digits)) 
       i3d))) 

 (define palindrome-product-3digits-numbers (make-rotation-number-list) 
   (filter palindrome? product-3digits-numbers)) RotationNumber? (make-product-list))) 

 (define answer-4 
   (apply max palindrome-product-3digits-numbers)) (make-rotation-number-list))) 

 (printf "4: ~D~%" answer-4) 
 ```