プロジェクト

全般

プロフィール

Problem 4 » 履歴 » バージョン 3

Noppi, 2023/12/27 04:45

1 1 Noppi
[ホーム](https://redmine.noppi.jp) - [[Wiki|Project Euler]]
2
# [[Problem 4]]
3
4
```scheme
5
#!r6rs
6
#!chezscheme
7
8
(import (chezscheme))
9
10
(define (RotationNumber? num)
11 3 Noppi
  (let* ([num-string (number->string num)]
12
         [str-length (string-length num-string)]
13
         [count (div str-length 2)])
14
    (let loop ([index 0])
15 1 Noppi
      (cond
16
        [(<= count index) #t]
17 3 Noppi
        [(equal?
18
           (string-ref num-string index)
19
           (string-ref num-string (- str-length 1 index)))
20 1 Noppi
         (loop (add1 index))]
21 2 Noppi
        [else #f]))))
22 1 Noppi
23
(define answer-4
24 3 Noppi
  (let loop1 ([num1 100] [result 0])
25 1 Noppi
    (if (< 999 num1)
26
      result
27 3 Noppi
      (let loop2 ([num2 100] [result result])
28 1 Noppi
        (if (< 999 num2)
29
          (loop1 (add1 num1) result)
30 3 Noppi
          (let ([num (* num1 num2)])
31 1 Noppi
            (if (RotationNumber? num)
32
              (loop2 (add1 num2) (max result num))
33
              (loop2 (add1 num2) result))))))))
34
35
(printf "4: ~D~%" answer-4)
36
```