Problem 58 » 履歴 » バージョン 1
Noppi, 2024/01/23 05:21
1 | 1 | Noppi | [ホーム](https://redmine.noppi.jp) - [[Wiki|Project Euler]] |
---|---|---|---|
2 | # [[Problem 58]] |
||
3 | |||
4 | ## Spiral Primes |
||
5 | Starting with $1$ and spiralling anticlockwise in the following way, a square spiral with side length $7$ is formed. |
||
6 | |||
7 | | | | | | | | | |
||
8 | |--|--|--|--|--|--|--| |
||
9 | | <span style="color:red">**37**</span> | 36 | 35 | 34 | 33 | 32 | <span style="color:red">**31**</span> | |
||
10 | | 38 | <span style="color:red">**17**</span> | 16 | 15 | 14 | <span style="color:red">**13**</span> | 30 | |
||
11 | | 39 | 18 | <span style="color:red">**5**</span> | 4 | <span style="color:red">**3**</span> | 12 | 29 | |
||
12 | | 40 | 19 | 6 | 1 | 2 | 11 | 28 | |
||
13 | | 41 | 20 | <span style="color:red">**7**</span> | 8 | 9 | 10 | 27 | |
||
14 | | 42 | 21 | 22 | 23 | 24 | 25 | 26 | |
||
15 | | <span style="color:red">**43**</span> | 44 | 45 | 46 | 47 | 48 | 49 | |
||
16 | |||
17 | It is interesting to note that the odd squares lie along the bottom right diagonal, but what is more interesting is that $8$ out of the $13$ numbers lying along both diagonals are prime; that is, a ratio of $8/13 \approx 62$ %. |
||
18 | |||
19 | If one complete new layer is wrapped around the spiral above, a square spiral with side length $9$ will be formed. If this process is continued, what is the side length of the square spiral for which the ratio of primes along both diagonals first falls below $10\%$? |
||
20 | |||
21 | ## 螺旋素数 |
||
22 | 1から始めて, 以下のように反時計回りに数字を並べていくと, 辺の長さが7の渦巻きが形成される. |
||
23 | |||
24 | | | | | | | | | |
||
25 | |--|--|--|--|--|--|--| |
||
26 | | <span style="color:red">**37**</span> | 36 | 35 | 34 | 33 | 32 | <span style="color:red">**31**</span> | |
||
27 | | 38 | <span style="color:red">**17**</span> | 16 | 15 | 14 | <span style="color:red">**13**</span> | 30 | |
||
28 | | 39 | 18 | <span style="color:red">**5**</span> | 4 | <span style="color:red">**3**</span> | 12 | 29 | |
||
29 | | 40 | 19 | 6 | 1 | 2 | 11 | 28 | |
||
30 | | 41 | 20 | <span style="color:red">**7**</span> | 8 | 9 | 10 | 27 | |
||
31 | | 42 | 21 | 22 | 23 | 24 | 25 | 26 | |
||
32 | | <span style="color:red">**43**</span> | 44 | 45 | 46 | 47 | 48 | 49 | |
||
33 | |||
34 | 面白いことに, 奇平方数が右下の対角線上に出現する. もっと面白いことには, 対角線上の13個の数字のうち, 8個が素数である. ここで割合は$8/13 \approx 62$ %である. |
||
35 | |||
36 | 渦巻きに新しい層を付け加えよう. すると辺の長さが9の渦巻きが出来る. 以下, この操作を繰り返していく. 対角線上の素数の割合が10%未満に落ちる最初の辺の長さを求めよ. |
||
37 | |||
38 | ```scheme |
||
39 | (import (scheme base) |
||
40 | (gauche base)) |
||
41 | |||
42 | (define (prime? num) |
||
43 | (assume (exact-integer? num)) |
||
44 | (assume (positive? num)) |
||
45 | (cond |
||
46 | [(= num 1) #f] |
||
47 | [(= num 2) #t] |
||
48 | [(even? num) #f] |
||
49 | [(= num 3) #t] |
||
50 | [(zero? (mod num 3)) #f] |
||
51 | [else |
||
52 | (let loop ([n6-1 5]) |
||
53 | (let ([n6+1 (+ n6-1 2)]) |
||
54 | (cond |
||
55 | [(< num |
||
56 | (* n6-1 n6-1)) |
||
57 | #t] |
||
58 | [(zero? (mod num n6-1)) |
||
59 | #f] |
||
60 | [(< num |
||
61 | (* n6+1 n6+1)) |
||
62 | #t] |
||
63 | [(zero? (mod num n6+1)) |
||
64 | #f] |
||
65 | [else |
||
66 | (loop (+ n6+1 4))])))])) |
||
67 | |||
68 | (define edge-p |
||
69 | (let loop ([edge 3] |
||
70 | [init 3] |
||
71 | [all 1] |
||
72 | [prime-count 0] |
||
73 | [result '()]) |
||
74 | (let* ([inc (- edge 1)] |
||
75 | [n1 init] |
||
76 | [n2 (+ n1 inc)] |
||
77 | [n3 (+ n2 inc)] |
||
78 | [n4 (+ n3 inc)] |
||
79 | [next-all (+ all 4)] |
||
80 | [next-prime-count prime-count]) |
||
81 | (when (prime? n1) |
||
82 | (set! next-prime-count (+ next-prime-count 1))) |
||
83 | (when (prime? n2) |
||
84 | (set! next-prime-count (+ next-prime-count 1))) |
||
85 | (when (prime? n3) |
||
86 | (set! next-prime-count (+ next-prime-count 1))) |
||
87 | (when (prime? n4) |
||
88 | (set! next-prime-count (+ next-prime-count 1))) |
||
89 | (let ([p (/ (* next-prime-count 100.0) |
||
90 | next-all)]) |
||
91 | (if (< p 10) |
||
92 | (reverse (cons `(,edge . ,p) result)) |
||
93 | (loop (+ edge 2) |
||
94 | (+ n4 edge 1) |
||
95 | next-all |
||
96 | next-prime-count |
||
97 | (cons `(,edge . ,p) result))))))) |
||
98 | |||
99 | (define answer-58 |
||
100 | (car (last edge-p))) |
||
101 | |||
102 | (format #t "58: ~d~%" answer-58) |
||
103 | ``` |