Problem 60 » 履歴 » バージョン 2
Noppi, 2024/01/26 06:32
1 | 1 | Noppi | [ホーム](https://redmine.noppi.jp) - [[Wiki|Project Euler]] |
---|---|---|---|
2 | # [[Problem 60]] |
||
3 | |||
4 | ## Prime Pair Sets |
||
5 | The primes $3$, $7$, $109$, and $673$, are quite remarkable. By taking any two primes and concatenating them in any order the result will always be prime. For example, taking $7$ and $109$, both $7109$ and $1097$ are prime. The sum of these four primes, $792$, represents the lowest sum for a set of four primes with this property. |
||
6 | |||
7 | Find the lowest sum for a set of five primes for which any two primes concatenate to produce another prime. |
||
8 | |||
9 | ## 素数ペア集合 |
||
10 | 素数3, 7, 109, 673は非凡な性質を持っている. 任意の2つの素数を任意の順で繋げると, また素数になっている. 例えば, 7と109を用いると, 7109と1097の両方が素数である. これら4つの素数の和は792である. これは, このような性質をもつ4つの素数の集合の和の中で最小である. |
||
11 | |||
12 | 任意の2つの素数を繋げたときに別の素数が生成される, 5つの素数の集合の和の中で最小のものを求めよ. |
||
13 | |||
14 | ```scheme |
||
15 | 2 | Noppi | (import (scheme base) |
16 | (gauche base) |
||
17 | (scheme inexact) |
||
18 | (scheme list)) |
||
19 | |||
20 | (define (prime? num) |
||
21 | (assume (exact-integer? num)) |
||
22 | (assume (positive? num)) |
||
23 | (cond |
||
24 | [(= num 1) #f] |
||
25 | [(= num 2) #t] |
||
26 | [(even? num) #f] |
||
27 | [(= num 3) #t] |
||
28 | [(zero? (mod num 3)) #f] |
||
29 | [else |
||
30 | (let loop ([n6-1 5]) |
||
31 | (let ([n6+1 (+ n6-1 2)]) |
||
32 | (cond |
||
33 | [(< num |
||
34 | (* n6-1 n6-1)) |
||
35 | #t] |
||
36 | [(zero? (mod num n6-1)) |
||
37 | #f] |
||
38 | [(< num |
||
39 | (* n6+1 n6+1)) |
||
40 | #t] |
||
41 | [(zero? (mod num n6+1)) |
||
42 | #f] |
||
43 | [else |
||
44 | (loop (+ n6+1 4))])))])) |
||
45 | |||
46 | (define prime-generator |
||
47 | (let ([current-primes '(2 3 5 7)] |
||
48 | [inc 4] |
||
49 | [prime? (^[num current-primes] |
||
50 | (assume (exact-integer? num)) |
||
51 | (assume (< 7 num)) |
||
52 | (let loop ([rest-primes current-primes]) |
||
53 | (let ([rest (car rest-primes)]) |
||
54 | (cond |
||
55 | [(< num |
||
56 | (* rest rest)) |
||
57 | #t] |
||
58 | [(zero? (mod num rest)) |
||
59 | #f] |
||
60 | [else |
||
61 | (loop (cdr rest-primes))]))))]) |
||
62 | (lambda () |
||
63 | (let loop ([next-prime (+ (last current-primes) |
||
64 | inc)] |
||
65 | [next-inc (if (= inc 2) |
||
66 | 4 |
||
67 | 2)]) |
||
68 | (cond |
||
69 | [(prime? next-prime current-primes) |
||
70 | (set! current-primes (append current-primes `(,next-prime))) |
||
71 | (set! inc next-inc) |
||
72 | next-prime] |
||
73 | [else |
||
74 | (loop (+ next-prime next-inc) |
||
75 | (if (= next-inc 2) |
||
76 | 4 |
||
77 | 2))]))))) |
||
78 | |||
79 | (define prime-list (generator->lseq 3 7 |
||
80 | prime-generator)) |
||
81 | |||
82 | (define (digit-num num) |
||
83 | (+ (floor->exact (log num 10)) |
||
84 | 1)) |
||
85 | |||
86 | (define (perm-number lis num) |
||
87 | (let loop ([rest lis] [result '()]) |
||
88 | (if (null? rest) |
||
89 | result |
||
90 | (let* ([current (car rest)] |
||
91 | [num1 (+ (* num (expt 10 (digit-num current))) |
||
92 | current)] |
||
93 | [num2 (+ (* current (expt 10 (digit-num num))) |
||
94 | num)]) |
||
95 | (loop (cdr rest) (cons* num1 num2 result)))))) |
||
96 | |||
97 | (define (perm-number-all-prime? lis num) |
||
98 | (every prime? (perm-number lis num))) |
||
99 | |||
100 | (define (perm-prime-list num) |
||
101 | (let ([target-prime-list (take-while (cut < <> num) |
||
102 | prime-list)]) |
||
103 | (let loop ([rest-primes target-prime-list] |
||
104 | [candidate '()]) |
||
105 | (cond |
||
106 | [(null? rest-primes) #f] |
||
107 | [(<= 5 (length candidate)) |
||
108 | candidate] |
||
109 | [else |
||
110 | (let ([next-candidate-list (filter (^[lis] |
||
111 | (if (null? (cdr lis)) |
||
112 | #t |
||
113 | (perm-number-all-prime? (cdr lis) (car lis)))) |
||
114 | (map (^n (cons n candidate)) |
||
115 | rest-primes))]) |
||
116 | (any (^[lis] |
||
117 | (loop (delete (car lis) rest-primes) |
||
118 | (cons (car lis) candidate))) |
||
119 | next-candidate-list))])))) |
||
120 | |||
121 | (define answer-60 |
||
122 | (apply + (perm-prime-list 10000))) |
||
123 | |||
124 | (format #t "60: ~d~%" answer-60) |
||
125 | 1 | Noppi | ``` |