Problem 61 » 履歴 » バージョン 2
Noppi, 2024/01/27 13:01
1 | 1 | Noppi | [ホーム](https://redmine.noppi.jp) - [[Wiki|Project Euler]] |
---|---|---|---|
2 | # [[Problem 61]] |
||
3 | |||
4 | ## Cyclical Figurate Numbers |
||
5 | Triangle, square, pentagonal, hexagonal, heptagonal, and octagonal numbers are all figurate (polygonal) numbers and are generated by the following formulae: |
||
6 | |||
7 | | | | | |
||
8 | |--|--|--| |
||
9 | | Triangle | $P_{3,n}=n(n+1)/2$ | $1, 3, 6, 10, 15, \dots$ | |
||
10 | | Square | $P_{4,n}=n^2$ | $1, 4, 9, 16, 25, \dots$ | |
||
11 | | Pentagonal | $P_{5,n}=n(3n-1)/2$ | $1, 5, 12, 22, 35, \dots$ | |
||
12 | | Hexagonal | $P_{6,n}=n(2n-1)$ | $1, 6, 15, 28, 45, \dots$ | |
||
13 | | Heptagonal | $P_{7,n}=n(5n-3)/2$ | $1, 7, 18, 34, 55, \dots$ | |
||
14 | | Octagonal | $P_{8,n}=n(3n-2)$ | $1, 8, 21, 40, 65, \dots$ | |
||
15 | |||
16 | The ordered set of three $4$-digit numbers: $8128$, $2882$, $8281$, has three interesting properties. |
||
17 | |||
18 | 1. The set is cyclic, in that the last two digits of each number is the first two digits of the next number (including the last number with the first). |
||
19 | 1. Each polygonal type: triangle ($P_{3,127}=8128$), square ($P_{4,91}=8281$), and pentagonal ($P_{5,44}=2882$), is represented by a different number in the set. |
||
20 | 1. This is the only set of $4$-digit numbers with this property. |
||
21 | |||
22 | Find the sum of the only ordered set of six cyclic $4$-digit numbers for which each polygonal type: triangle, square, pentagonal, hexagonal, heptagonal, and octagonal, is represented by a different number in the set. |
||
23 | |||
24 | ## 巡回図形数 |
||
25 | 三角数, 四角数, 五角数, 六角数, 七角数, 八角数は多角数であり, それぞれ以下の式で生成される. |
||
26 | |||
27 | | | | | |
||
28 | |--|--|--| |
||
29 | | 三角数 | $P_{3,n}=n(n+1)/2$ | $1, 3, 6, 10, 15, \dots$ | |
||
30 | | 四角数 | $P_{4,n}=n^2$ | $1, 4, 9, 16, 25, \dots$ | |
||
31 | | 五角数 | $P_{5,n}=n(3n-1)/2$ | $1, 5, 12, 22, 35, \dots$ | |
||
32 | | 六角数 | $P_{6,n}=n(2n-1)$ | $1, 6, 15, 28, 45, \dots$ | |
||
33 | | 七角数 | $P_{7,n}=n(5n-3)/2$ | $1, 7, 18, 34, 55, \dots$ | |
||
34 | | 八角数 | $P_{8,n}=n(3n-2)$ | $1, 8, 21, 40, 65, \dots$ | |
||
35 | |||
36 | 3つの4桁の数の順番付きの集合 (8128, 2882, 8281) は以下の面白い性質を持つ. |
||
37 | |||
38 | 1. この集合は巡回的である. 最後の数も含めて, 各数の後半2桁は次の数の前半2桁と一致する |
||
39 | 1. それぞれ多角数である: 三角数 ($P_{3,127}=8128$), 四角数 ($P_{4,91}=8281$), 五角数 ($P_{5,44}=2882$) がそれぞれ別の数字で集合に含まれている |
||
40 | 1. $4$桁の数の組で上の2つの性質を持つのはこの組だけである. |
||
41 | |||
42 | 三角数, 四角数, 五角数, 六角数, 七角数, 八角数が全て表れる6つの巡回する4桁の数からなる唯一の順序集合の和を求めよ. |
||
43 | |||
44 | ```scheme |
||
45 | 2 | Noppi | (import (scheme base) |
46 | (gauche base)) |
||
47 | |||
48 | (define (triangle-num num) |
||
49 | (let loop ([index 1] [result '()]) |
||
50 | (let ([current (/ (* index (+ index 1)) |
||
51 | 2)]) |
||
52 | (if (<= num current) |
||
53 | result |
||
54 | (loop (+ index 1) |
||
55 | (cons current result)))))) |
||
56 | |||
57 | (define (square-num num) |
||
58 | (let loop ([index 1] [result '()]) |
||
59 | (let ([current (* index index)]) |
||
60 | (if (<= num current) |
||
61 | result |
||
62 | (loop (+ index 1) |
||
63 | (cons current result)))))) |
||
64 | |||
65 | (define (pentagonal-num num) |
||
66 | (let loop ([index 1] [result '()]) |
||
67 | (let ([current (/ (* index |
||
68 | (- (* index 3) |
||
69 | 1)) |
||
70 | 2)]) |
||
71 | (if (<= num current) |
||
72 | result |
||
73 | (loop (+ index 1) |
||
74 | (cons current result)))))) |
||
75 | |||
76 | (define (hexagonal-num num) |
||
77 | (let loop ([index 1] [result '()]) |
||
78 | (let ([current (* index |
||
79 | (- (* index 2) |
||
80 | 1))]) |
||
81 | (if (<= num current) |
||
82 | result |
||
83 | (loop (+ index 1) |
||
84 | (cons current result)))))) |
||
85 | |||
86 | (define (heptagonal-num num) |
||
87 | (let loop ([index 1] [result '()]) |
||
88 | (let ([current (/ (* index |
||
89 | (- (* index 5) |
||
90 | 3)) |
||
91 | 2)]) |
||
92 | (if (<= num current) |
||
93 | result |
||
94 | (loop (+ index 1) |
||
95 | (cons current result)))))) |
||
96 | |||
97 | (define (octagonal-num num) |
||
98 | (let loop ([index 1] [result '()]) |
||
99 | (let ([current (* index |
||
100 | (- (* index 3) |
||
101 | 2))]) |
||
102 | (if (<= num current) |
||
103 | result |
||
104 | (loop (+ index 1) |
||
105 | (cons current result)))))) |
||
106 | |||
107 | (define (four-digit-list proc) |
||
108 | (filter (^n |
||
109 | (and (<= 1000 n 9999) |
||
110 | (<= 10 (mod n 100)))) |
||
111 | (proc 10000))) |
||
112 | |||
113 | (define (possible-4digit-list) |
||
114 | (let loop ([rest `((3 . ,triangle-num) |
||
115 | (4 . ,square-num) |
||
116 | (5 . ,pentagonal-num) |
||
117 | (6 . ,hexagonal-num) |
||
118 | (7 . ,heptagonal-num) |
||
119 | (8 . ,octagonal-num))] |
||
120 | [result '()]) |
||
121 | (if (null? rest) |
||
122 | (reverse result) |
||
123 | (let ([base (caar rest)] |
||
124 | [proc (cdar rest)]) |
||
125 | (loop (cdr rest) |
||
126 | (append |
||
127 | (map (cut cons base <>) |
||
128 | (four-digit-list proc)) |
||
129 | result)))))) |
||
130 | |||
131 | (define (delete-same-base cell lis) |
||
132 | (let ([base (car cell)]) |
||
133 | (filter (^c (not (= base (car c)))) |
||
134 | lis))) |
||
135 | |||
136 | (define (findall-next-num num lis) |
||
137 | (assume exact-integer? num) |
||
138 | (assume (<= 1010 num 9999)) |
||
139 | (let ([lower (+ (* (mod num 100) 100) 10)] |
||
140 | [upper (+ (* (mod num 100) 100) 99)]) |
||
141 | (filter (^c (<= lower (cdr c) upper)) |
||
142 | lis))) |
||
143 | |||
144 | (define (find-cyclical-num) |
||
145 | (let loop ([rest (possible-4digit-list)] |
||
146 | [result '()]) |
||
147 | (cond |
||
148 | [(null? rest) |
||
149 | (assume (not (null? result))) |
||
150 | (and (not (null? (findall-next-num |
||
151 | (cdar result) |
||
152 | (last-pair result)))) |
||
153 | (reverse result))] |
||
154 | [(null? result) |
||
155 | (assume (not (null? rest))) |
||
156 | (any (^c |
||
157 | (loop (delete-same-base c rest) |
||
158 | (cons c result))) |
||
159 | rest)] |
||
160 | [else |
||
161 | (let ([candidate (findall-next-num (cdar result) rest)]) |
||
162 | (and (not (null? candidate)) |
||
163 | (any (^c |
||
164 | (loop (delete-same-base c rest) |
||
165 | (cons c result))) |
||
166 | candidate)))]))) |
||
167 | |||
168 | (define answer-61 |
||
169 | (apply + |
||
170 | (map (cut cdr <>) |
||
171 | (find-cyclical-num)))) |
||
172 | |||
173 | (format #t "61: ~d~%" answer-61) |
||
174 | 1 | Noppi | ``` |