Problem 46 » 履歴 » バージョン 1
Noppi, 2024/01/16 13:10
1 | 1 | Noppi | [ホーム](https://redmine.noppi.jp) - [[Wiki|Project Euler]] |
---|---|---|---|
2 | # [[Problem 46]] |
||
3 | |||
4 | ## Goldbach's Other Conjecture |
||
5 | <p>It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square.</p> |
||
6 | \begin{align} |
||
7 | 9 = 7 + 2 \times 1^2\\ |
||
8 | 15 = 7 + 2 \times 2^2\\ |
||
9 | 21 = 3 + 2 \times 3^2\\ |
||
10 | 25 = 7 + 2 \times 3^2\\ |
||
11 | 27 = 19 + 2 \times 2^2\\ |
||
12 | 33 = 31 + 2 \times 1^2 |
||
13 | \end{align} |
||
14 | |||
15 | It turns out that the conjecture was false. |
||
16 | |||
17 | What is the smallest odd composite that cannot be written as the sum of a prime and twice a square? |
||
18 | |||
19 | ## もうひとつのゴールドバッハの予想 |
||
20 | <p>Christian Goldbachは全ての奇合成数は平方数の2倍と素数の和で表せると予想した.</p> |
||
21 | \begin{align} |
||
22 | 9 = 7 + 2 \times 1^2\\ |
||
23 | 15 = 7 + 2 \times 2^2\\ |
||
24 | 21 = 3 + 2 \times 3^2\\ |
||
25 | 25 = 7 + 2 \times 3^2\\ |
||
26 | 27 = 19 + 2 \times 2^2\\ |
||
27 | 33 = 31 + 2 \times 1^2 |
||
28 | \end{align} |
||
29 | |||
30 | 後に, この予想は誤りであることが分かった. |
||
31 | |||
32 | 平方数の2倍と素数の和で表せない最小の奇合成数はいくつか? |
||
33 | |||
34 | ```scheme |
||
35 | (import (scheme base) |
||
36 | (gauche base)) |
||
37 | |||
38 | (define (prime? num) |
||
39 | (assume (exact-integer? num)) |
||
40 | (assume (positive? num)) |
||
41 | (cond |
||
42 | [(= num 1) #f] |
||
43 | [(= num 2) #t] |
||
44 | [(even? num) #f] |
||
45 | [(= num 3) #t] |
||
46 | [(zero? (mod num 3)) #f] |
||
47 | [else |
||
48 | (let loop ([i 1]) |
||
49 | (let ([n6-1 (- (* i 6) 1)] |
||
50 | [n6+1 (+ (* i 6) 1)]) |
||
51 | (cond |
||
52 | [(< num |
||
53 | (* n6-1 n6-1)) |
||
54 | #t] |
||
55 | [(zero? (mod num n6-1)) |
||
56 | #f] |
||
57 | [(< num |
||
58 | (* n6+1 n6+1)) |
||
59 | #t] |
||
60 | [(zero? (mod num n6+1)) |
||
61 | #f] |
||
62 | [else |
||
63 | (loop (+ i 1))])))])) |
||
64 | |||
65 | (define (goldbach? num) |
||
66 | (assume (exact-integer? num)) |
||
67 | (assume (<= 9 num)) |
||
68 | (let loop ([i 1]) |
||
69 | (let ([square-i*2 (* (* i i) |
||
70 | 2)]) |
||
71 | (cond |
||
72 | [(< num square-i*2) #f] |
||
73 | [(prime? (- num |
||
74 | square-i*2)) |
||
75 | #t] |
||
76 | [else |
||
77 | (loop (+ i 1))])))) |
||
78 | |||
79 | (define goldbach-false-num |
||
80 | (let loop ([i 35]) |
||
81 | (cond |
||
82 | [(prime? i) (loop (+ i 2))] |
||
83 | [(goldbach? i) (loop (+ i 2))] |
||
84 | [else i]))) |
||
85 | |||
86 | (define answer-46 goldbach-false-num) |
||
87 | |||
88 | (format #t "46: ~d~%" answer-46) |
||
89 | ``` |