プロジェクト

全般

プロフィール

操作

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

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

(define 3digits-combinations
  (fold-left
    (lambda (lis current)
      (let loop ([result lis]
                 [temp (map
                         (lambda (n) `(,current ,n))
                         iota-3digits)])
        (if (null? temp)
          result
          (loop (cons (car temp) result) (cdr temp)))))
    '()
    iota-3digits))

(define product-3digits-numbers
  (map
    (lambda (lis) (apply * lis))
    3digits-combinations))

(define palindrome-product-3digits-numbers
  (filter palindrome? product-3digits-numbers))

(define answer-4
  (apply max palindrome-product-3digits-numbers))

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

Noppi2023/12/29に更新 · 6件の履歴