Problem 37 » 履歴 » バージョン 2
Noppi, 2024/01/14 13:40
1 | 1 | Noppi | [ホーム](https://redmine.noppi.jp) - [[Wiki|Project Euler]] |
---|---|---|---|
2 | # [[Problem 37]] |
||
3 | |||
4 | ## Truncatable Primes |
||
5 | The number $3797$ has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: $3797$, $797$, $97$, and $7$. Similarly we can work from right to left: $3797$, $379$, $37$, and $3$. |
||
6 | |||
7 | Find the sum of the only eleven primes that are both truncatable from left to right and right to left. |
||
8 | |||
9 | NOTE: $2$, $3$, $5$, and $7$ are not considered to be truncatable primes. |
||
10 | |||
11 | ## 切り詰め可能素数 |
||
12 | 3797は面白い性質を持っている. まずそれ自身が素数であり, 左から右に桁を除いたときに全て素数になっている (3797, 797, 97, 7). 同様に右から左に桁を除いたときも全て素数である (3797, 379, 37, 3). |
||
13 | |||
14 | 右から切り詰めても左から切り詰めても素数になるような素数は11個しかない. 総和を求めよ. |
||
15 | |||
16 | 注: 2, 3, 5, 7を切り詰め可能な素数とは考えない. |
||
17 | |||
18 | ```scheme |
||
19 | 2 | Noppi | (import (scheme base) |
20 | (gauche base) |
||
21 | (scheme flonum) |
||
22 | (math prime) |
||
23 | (scheme list)) |
||
24 | |||
25 | (define temp-primes (primes)) |
||
26 | |||
27 | (define (number-of-digit n) |
||
28 | (assume (exact-integer? n)) |
||
29 | (assume (<= 0 n)) |
||
30 | (if (zero? n) |
||
31 | 1 |
||
32 | (+ (floor->exact |
||
33 | (fllog10 n)) |
||
34 | 1))) |
||
35 | |||
36 | (define (left-trancate n) |
||
37 | (assume (exact-integer? n)) |
||
38 | (assume (<= 10 n)) |
||
39 | (let* ([dn (number-of-digit n)] |
||
40 | [module-num (expt 10 |
||
41 | (- dn 1))]) |
||
42 | (mod n module-num))) |
||
43 | |||
44 | (define (right-trancate n) |
||
45 | (assume (exact-integer? n)) |
||
46 | (assume (<= 10 n)) |
||
47 | (div n 10)) |
||
48 | |||
49 | (define (trancates-list n proc) |
||
50 | (assume (exact-integer? n)) |
||
51 | (assume (<= 0 n)) |
||
52 | (if (< n 10) |
||
53 | '() |
||
54 | (let loop ([current n] [result `(,n)]) |
||
55 | (if (< current 10) |
||
56 | result |
||
57 | (let ([next (proc current)]) |
||
58 | (loop next |
||
59 | (cons next result))))))) |
||
60 | |||
61 | (define (prime? n) |
||
62 | (= (car |
||
63 | (drop-while (cut < <> n) |
||
64 | temp-primes)) |
||
65 | n)) |
||
66 | |||
67 | (define all-trancate-nums |
||
68 | (let loop ([find-count 0] [rest-primes temp-primes] [result '()]) |
||
69 | (if (<= 11 find-count) |
||
70 | result |
||
71 | (let ([current-prime (car rest-primes)] |
||
72 | [rest-primes (cdr rest-primes)]) |
||
73 | (cond |
||
74 | [(< current-prime 10) |
||
75 | (loop find-count rest-primes result)] |
||
76 | [(and (every prime? |
||
77 | (trancates-list current-prime left-trancate)) |
||
78 | (every prime? |
||
79 | (trancates-list current-prime right-trancate))) |
||
80 | (loop (+ find-count 1) |
||
81 | rest-primes |
||
82 | (cons current-prime result))] |
||
83 | [else (loop find-count rest-primes result)]))))) |
||
84 | |||
85 | (define answer-37 |
||
86 | (apply + all-trancate-nums)) |
||
87 | |||
88 | (format #t "37: ~d~%" answer-37) |
||
89 | 1 | Noppi | ``` |