プロジェクト

全般

プロフィール

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

Noppi, 2024/01/16 04:54

1 1 Noppi
[ホーム](https://redmine.noppi.jp) - [[Wiki|Project Euler]]
2
# [[Problem 43]]
3
4
## Sub-string Divisibility
5
The number, $1406357289$, is a $0$ to $9$ pandigital number because it is made up of each of the digits $0$ to $9$ in some order, but it also has a rather interesting sub-string divisibility property.
6
7
Let $d_1$ be the $1$<sup>st</sup> digit, $d_2$ be the $2$<sup>nd</sup> digit, and so on. In this way, we note the following:
8
9
* $d_2d_3d_4=406$ is divisible by $2$
10
* $d_3d_4d_5=063$ is divisible by $3$
11
* $d_4d_5d_6=635$ is divisible by $5$
12
* $d_5d_6d_7=357$ is divisible by $7$
13
* $d_6d_7d_8=572$ is divisible by $11$
14
* $d_7d_8d_9=728$ is divisible by $13$
15
* $d_8d_9d_{10}=289$ is divisible by $17$
16
17
Find the sum of all $0$ to $9$ pandigital numbers with this property.
18
19
## 部分文字列被整除性
20
数1406357289は0から9のパンデジタル数である (0から9が1度ずつ現れるので). この数は部分文字列が面白い性質を持っている.
21
22
$d_1$を上位1桁目, $d_2$を上位2桁目の数とし, 以下順に$d_n$を定義する. この記法を用いると次のことが分かる.
23
24
* $d_2d_3d_4=406$ は 2 で割り切れる
25
* $d_3d_4d_5=063$ は 3 で割り切れる
26
* $d_4d_5d_6=635$ は 5 で割り切れる
27
* $d_5d_6d_7=357$ は 7 で割り切れる
28
* $d_6d_7d_8=572$ は 11 で割り切れる
29
* $d_7d_8d_9=728$ は 13 で割り切れる
30
* $d_8d_9d_{10}=289$ は 17 で割り切れる
31
32
このような性質をもつ0から9のパンデジタル数の総和を求めよ.
33
34
```scheme
35 2 Noppi
(import (scheme base)
36
        (gauche base))
37
38
(define pandigital-num-list
39
  (let loop ([index 1]
40
             [rest (iota 10)]
41
             [current 0]
42
             [lis '()])
43
    (let ([next-loop (^[fold-rest]
44
                       (fold-right (^[n lis]
45
                                     (loop (+ index 1)
46
                                           (delete n rest)
47
                                           (+ (* current 10)
48
                                              n)
49
                                           lis))
50
                                   lis
51
                                   fold-rest))])
52
      (cond
53
        [(null? rest)
54
         (cons current lis)]
55
        ; 先頭は 0 以外
56
        [(= index 1)
57
         (next-loop (iota 9 1))]
58
        ; 先頭から 4 桁目は偶数になる
59
        [(= index 4)
60
         (let ([rest-even (filter even? rest)])
61
           (if (null? rest-even)
62
             lis
63
             (next-loop rest-even)))]
64
        ; 先頭から 6 桁目は 0 または 5 になる
65
        [(= index 6)
66
         (let ([rest-5 (filter (^n (zero? (mod n 5)))
67
                               rest)])
68
           (if (null? rest-5)
69
             lis
70
             (next-loop rest-5)))]
71
        [else
72
          (next-loop rest)]))))
73
74
(define (match-num? num)
75
  (filter (^n
76
            (let ([d345 (div (mod n 100_000_000)
77
                             100_000)]
78
                  [d567 (div (mod n 1_000_000)
79
                             1_000)]
80
                  [d678 (div (mod n 100_000)
81
                             100)]
82
                  [d789 (div (mod n 10_000)
83
                             10)]
84
                  [d890 (mod n 1_000)])
85
              (and (zero? (mod d345 3))
86
                   (zero? (mod d567 7))
87
                   (zero? (mod d678 11))
88
                   (zero? (mod d789 13))
89
                   (zero? (mod d890 17)))))
90
          pandigital-num-list))
91
92
(define answer-43
93
  (apply + (match-num? pandigital-num-list)))
94
95
(format #t "43: ~d~%" answer-43)
96 1 Noppi
```