プロジェクト

全般

プロフィール

Problem 52 » 履歴 » バージョン 2

Noppi, 2024/01/18 13:06

1 1 Noppi
[ホーム](https://redmine.noppi.jp) - [[Wiki|Project Euler]]
2
# [[Problem 52]]
3
4
## Permuted Multiples
5
It can be seen that the number, $125874$, and its double, $251748$, contain exactly the same digits, but in a different order.
6
7
Find the smallest positive integer, $x$, such that $2x$, $3x$, $4x$, $5x$, and $6x$, contain the same digits.
8
9
## 置換倍数
10
125874を2倍すると251748となる. これは元の数125874と順番は違うが同じ数を含む.
11
12
2x, 3x, 4x, 5x, 6x が x と同じ数を含むような最小の正整数 x を求めよ.
13
14
```scheme
15 2 Noppi
(import (scheme base)
16
        (gauche base))
17
18
(define (number->list num)
19
  (assume (exact-integer? num))
20
  (assume (<= 0 num))
21
  (if (zero? num)
22
    '(0)
23
    (let loop ([rest num] [lis '()])
24
      (if (zero? rest)
25
        lis
26
        (loop (div rest 10)
27
              (cons (mod rest 10)
28
                    lis))))))
29
30
(define perm-num?
31
  (case-lambda
32
    [(num1 num2)
33
     (let ([num-list1 (number->list num1)]
34
           [num-list2 (number->list num2)])
35
       (if (not (= (length num-list1)
36
                   (length num-list2)))
37
         #f
38
         (let loop ([rest-1 num-list1]
39
                    [rest-2 num-list2])
40
           (cond
41
             [(null? rest-1) #t]
42
             [(find (cut = <> (car rest-1))
43
                    rest-2)
44
              (loop (cdr rest-1)
45
                    (delete (car rest-1) rest-2))]
46
             [else #f]))))]
47
    [(num1 num2 . num3)
48
     (and (perm-num? num1 num2)
49
          (apply perm-num? num2 num3))]))
50
51
(define find-6digit
52
  (let loop ([num1 102345])
53
    (if (< 165987 num1)
54
      (errorf "解が見つかりませんでした~%")
55
      (let ([num2 (* num1 2)]
56
            [num3 (* num1 3)]
57
            [num4 (* num1 4)]
58
            [num5 (* num1 5)]
59
            [num6 (* num1 6)])
60
        (if (perm-num? num1 num2 num3 num4 num5 num6)
61
          num1
62
          (loop (+ num1 1)))))))
63
64
(define answer-52 find-6digit)
65
66
(format #t "52: ~d~%" answer-52)
67 1 Noppi
```