Problem 49 » 履歴 » バージョン 2
Noppi, 2024/01/17 06:02
| 1 | 1 | Noppi | [ホーム](https://redmine.noppi.jp) - [[Wiki|Project Euler]] |
|---|---|---|---|
| 2 | # [[Problem 49]] |
||
| 3 | |||
| 4 | ## Prime Permutations |
||
| 5 | The arithmetic sequence, $1487, 4817, 8147$, in which each of the terms increases by $3330$, is unusual in two ways: (i) each of the three terms are prime, and, (ii) each of the $4$-digit numbers are permutations of one another. |
||
| 6 | |||
| 7 | There are no arithmetic sequences made up of three $1$-, $2$-, or $3$-digit primes, exhibiting this property, but there is one other $4$-digit increasing sequence. |
||
| 8 | |||
| 9 | What $12$-digit number do you form by concatenating the three terms in this sequence? |
||
| 10 | |||
| 11 | ## 素数数列 |
||
| 12 | 項差3330の等差数列$1487, 4817, 8147$は次の2つの変わった性質を持つ. |
||
| 13 | |||
| 14 | 1. 3つの項はそれぞれ素数である. |
||
| 15 | 1. 各項は他の項の置換で表される. |
||
| 16 | |||
| 17 | 1, 2, 3桁の素数にはこのような性質を持った数列は存在しないが, 4桁の増加列にはもう1つ存在する. |
||
| 18 | |||
| 19 | それではこの数列の3つの項を連結した12桁の数を求めよ. |
||
| 20 | |||
| 21 | ```scheme |
||
| 22 | 2 | Noppi | (import (scheme base) |
| 23 | (gauche base) |
||
| 24 | (scheme sort)) |
||
| 25 | |||
| 26 | (define (prime? num) |
||
| 27 | (assume (exact-integer? num)) |
||
| 28 | (assume (positive? num)) |
||
| 29 | (cond |
||
| 30 | [(= num 1) #f] |
||
| 31 | [(= num 2) #t] |
||
| 32 | [(even? num) #f] |
||
| 33 | [(= num 3) #t] |
||
| 34 | [(zero? (mod num 3)) #f] |
||
| 35 | [else |
||
| 36 | (let loop ([n6-1 5]) |
||
| 37 | (let ([n6+1 (+ n6-1 2)]) |
||
| 38 | (cond |
||
| 39 | [(< num |
||
| 40 | (* n6-1 n6-1)) |
||
| 41 | #t] |
||
| 42 | [(zero? (mod num n6-1)) |
||
| 43 | #f] |
||
| 44 | [(< num |
||
| 45 | (* n6+1 n6+1)) |
||
| 46 | #t] |
||
| 47 | [(zero? (mod num n6+1)) |
||
| 48 | #f] |
||
| 49 | [else |
||
| 50 | (loop (+ n6+1 4))])))])) |
||
| 51 | |||
| 52 | (define (integer->list num) |
||
| 53 | (assume (exact-integer? num)) |
||
| 54 | (assume (<= 0 num)) |
||
| 55 | (if (zero? num) |
||
| 56 | '(0) |
||
| 57 | (let loop ([rest num] [lis '()]) |
||
| 58 | (if (zero? rest) |
||
| 59 | lis |
||
| 60 | (loop (div rest 10) |
||
| 61 | (cons (mod rest 10) |
||
| 62 | lis)))))) |
||
| 63 | |||
| 64 | (define (first-delete n lis) |
||
| 65 | (let loop ([rest lis] [deleted #f] [result '()]) |
||
| 66 | (cond |
||
| 67 | [(null? rest) (reverse result)] |
||
| 68 | [(boolean deleted) |
||
| 69 | (loop (cdr rest) deleted (cons (car rest) |
||
| 70 | result))] |
||
| 71 | [(= n (car rest)) |
||
| 72 | (loop (cdr rest) #t result)] |
||
| 73 | [else |
||
| 74 | (loop (cdr rest) deleted (cons (car rest) |
||
| 75 | result))]))) |
||
| 76 | |||
| 77 | (define (permutation-4digits num) |
||
| 78 | (assume (exact-integer? num)) |
||
| 79 | (assume (<= 1000 num 9999)) |
||
| 80 | (list-sort |
||
| 81 | < |
||
| 82 | (delete-duplicates |
||
| 83 | (let loop ([index 1] |
||
| 84 | [rest (integer->list num)] |
||
| 85 | [current 0] |
||
| 86 | [lis '()]) |
||
| 87 | (let ([next-loop (^[rest-lis] |
||
| 88 | (fold-right (^[n lis] |
||
| 89 | (loop (+ index 1) |
||
| 90 | (first-delete n rest) |
||
| 91 | (+ (* current 10) |
||
| 92 | n) |
||
| 93 | lis)) |
||
| 94 | lis |
||
| 95 | rest-lis))]) |
||
| 96 | (cond |
||
| 97 | [(null? rest) (cons current lis)] |
||
| 98 | [(= index 1) |
||
| 99 | (next-loop (filter (complement zero?) rest))] |
||
| 100 | [else (next-loop rest)])))))) |
||
| 101 | |||
| 102 | (define prime-4digits |
||
| 103 | (filter prime? (iota 9000 1000))) |
||
| 104 | |||
| 105 | (define perm-4digits |
||
| 106 | (delete-duplicates |
||
| 107 | (map permutation-4digits prime-4digits))) |
||
| 108 | |||
| 109 | (define prime-perm-4digits |
||
| 110 | (map (cut filter prime? <>) |
||
| 111 | perm-4digits)) |
||
| 112 | |||
| 113 | (define more3kind-prime-perm-4digits |
||
| 114 | (filter (^[lis] (<= 3 (length lis))) |
||
| 115 | prime-perm-4digits)) |
||
| 116 | |||
| 117 | (define valid-sequence-list |
||
| 118 | (fold (^[lis result] |
||
| 119 | (let ([count (length lis)]) |
||
| 120 | (let loop1 ([i 0] [result result]) |
||
| 121 | (if (<= count (+ i 1)) |
||
| 122 | result |
||
| 123 | (let loop2 ([j (+ i 1)] [result result]) |
||
| 124 | (if (<= count j) |
||
| 125 | (loop1 (+ i 1) result) |
||
| 126 | (let* ([value-i (list-ref lis i)] |
||
| 127 | [value-j (list-ref lis j)] |
||
| 128 | [value-k (+ value-j |
||
| 129 | (- value-j |
||
| 130 | value-i))]) |
||
| 131 | (if (find (cut = value-k <>) lis) |
||
| 132 | (loop2 (+ j 1) |
||
| 133 | (cons (cons* value-i value-j value-k) |
||
| 134 | result)) |
||
| 135 | (loop2 (+ j 1) result))))))))) |
||
| 136 | '() |
||
| 137 | more3kind-prime-perm-4digits)) |
||
| 138 | |||
| 139 | (define answer-49 |
||
| 140 | (let ([result (car (filter (^[lis] (not (= (car lis) 1487))) |
||
| 141 | valid-sequence-list))]) |
||
| 142 | (+ (* (car result) |
||
| 143 | 1_0000_0000) |
||
| 144 | (* (cadr result) |
||
| 145 | 1_0000) |
||
| 146 | (cddr result)))) |
||
| 147 | |||
| 148 | (format #t "49: ~d~%" answer-49) |
||
| 149 | 1 | Noppi | ``` |