プロジェクト

全般

プロフィール

操作

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

リビジョン 1/6 | 次 »
Noppi, 2023/12/27 01:36


ホーム - Project Euler

Problem 4

#!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 (answer-4)
  (let loop1 ((num1 100)
              (result 0))
    (if (< 999 num1)
      result
      (let loop2 ((num2 100)
                  (result result))
        (if (< 999 num2)
          (loop1 (add1 num1) result)
          (let ((num (* num1 num2)))
            (if (RotationNumber? num)
              (loop2 (add1 num2) (max result num))
              (loop2 (add1 num2) result))))))))

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

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