プロジェクト

全般

プロフィール

操作

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

« 前 | リビジョン 4/6 (差分) | 次 »
Noppi, 2023/12/27 13:03


ホーム - 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桁の数の積で表される回文数の最大値を求めよ.

#!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)
  (filter
    (lambda (n) (<= 100 n))
    (iota 1000)))

(define (make-product-list)
  (let ([i3d (iota-3-digits)])
    (fold-left
      (lambda (lis current)
        (let loop ([result lis]
                   [temp (map
                           (lambda (n) (* n current))
                           i3d)])
          (if (null? temp)
            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)))

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

Noppi2023/12/27に更新 · 4件の履歴