Problem 50 » 履歴 » バージョン 1
Noppi, 2024/01/17 07:33
| 1 | 1 | Noppi | [ホーム](https://redmine.noppi.jp) - [[Wiki|Project Euler]] |
|---|---|---|---|
| 2 | # [[Problem 50]] |
||
| 3 | |||
| 4 | ## Consecutive Prime Sum |
||
| 5 | The prime $41$, can be written as the sum of six consecutive primes: |
||
| 6 | |||
| 7 | $$41 = 2 + 3 + 5 + 7 + 11 + 13.$$ |
||
| 8 | |||
| 9 | This is the longest sum of consecutive primes that adds to a prime below one-hundred. |
||
| 10 | |||
| 11 | The longest sum of consecutive primes below one-thousand that adds to a prime, contains $21$ terms, and is equal to $953$. |
||
| 12 | |||
| 13 | Which prime, below one-million, can be written as the sum of the most consecutive primes? |
||
| 14 | |||
| 15 | ## 連続する素数の和 |
||
| 16 | 素数41は6つの連続する素数の和として表せる: |
||
| 17 | |||
| 18 | $$41 = 2 + 3 + 5 + 7 + 11 + 13.$$ |
||
| 19 | |||
| 20 | 100未満の素数を連続する素数の和で表したときにこれが最長になる. |
||
| 21 | |||
| 22 | 同様に, 連続する素数の和で1000未満の素数を表したときに最長になるのは953で21項を持つ. |
||
| 23 | |||
| 24 | 100万未満の素数を連続する素数の和で表したときに最長になるのはどの素数か? |
||
| 25 | |||
| 26 | ```scheme |
||
| 27 | (import (scheme base) |
||
| 28 | (gauche base) |
||
| 29 | (math prime) |
||
| 30 | (scheme list)) |
||
| 31 | |||
| 32 | (define temp-primes (primes)) |
||
| 33 | |||
| 34 | (define (prime? num) |
||
| 35 | (assume (exact-integer? num)) |
||
| 36 | (assume (positive? num)) |
||
| 37 | (cond |
||
| 38 | [(= num 1) #f] |
||
| 39 | [(= num 2) #t] |
||
| 40 | [(even? num) #f] |
||
| 41 | [(= num 3) #t] |
||
| 42 | [(zero? (mod num 3)) #f] |
||
| 43 | [else |
||
| 44 | (let loop ([n6-1 5]) |
||
| 45 | (let ([n6+1 (+ n6-1 2)]) |
||
| 46 | (cond |
||
| 47 | [(< num |
||
| 48 | (* n6-1 n6-1)) |
||
| 49 | #t] |
||
| 50 | [(zero? (mod num n6-1)) |
||
| 51 | #f] |
||
| 52 | [(< num |
||
| 53 | (* n6+1 n6+1)) |
||
| 54 | #t] |
||
| 55 | [(zero? (mod num n6+1)) |
||
| 56 | #f] |
||
| 57 | [else |
||
| 58 | (loop (+ n6+1 4))])))])) |
||
| 59 | |||
| 60 | (define (longest-sequence num) |
||
| 61 | (let loop ([sum 0] [current-primes temp-primes] [lis '()]) |
||
| 62 | (if (<= num (+ sum (car current-primes))) |
||
| 63 | (reverse lis) |
||
| 64 | (loop (+ sum (car current-primes)) |
||
| 65 | (cdr current-primes) |
||
| 66 | (cons (car current-primes) lis))))) |
||
| 67 | |||
| 68 | (define (match-lists num lis) |
||
| 69 | (let loop ([lis lis] |
||
| 70 | [current-primes (drop-while (cut <= <> (apply max lis)) |
||
| 71 | temp-primes)] |
||
| 72 | [result (cons lis '())]) |
||
| 73 | (if (<= num |
||
| 74 | (+ (apply + (cdr lis)) |
||
| 75 | (car current-primes))) |
||
| 76 | (reverse result) |
||
| 77 | (loop `(,@(cdr lis) ,(car current-primes)) |
||
| 78 | (cdr current-primes) |
||
| 79 | (cons `(,@(cdr lis) ,(car current-primes)) |
||
| 80 | result))))) |
||
| 81 | |||
| 82 | (define (prime-sequence num) |
||
| 83 | (let loop ([initial-list (longest-sequence num)]) |
||
| 84 | (or (find (^[lis] |
||
| 85 | (prime? (apply + lis))) |
||
| 86 | (match-lists num initial-list)) |
||
| 87 | (loop (drop-right initial-list 1))))) |
||
| 88 | |||
| 89 | (define answer-50 |
||
| 90 | (apply + (prime-sequence 100_0000))) |
||
| 91 | |||
| 92 | (format #t "50: ~d~%" answer-50) |
||
| 93 | ``` |