Problem 7 » 履歴 » バージョン 5
Noppi, 2023/12/29 12:48
| 1 | 1 | Noppi | [ホーム](https://redmine.noppi.jp) - [[Wiki|Project Euler]] |
|---|---|---|---|
| 2 | # [[Problem 7]] |
||
| 3 | |||
| 4 | 3 | Noppi | ## $10001$st Prime |
| 5 | By listing the first six prime numbers: $2, 3, 5, 7, 11$, and $13$, we can see that the $6$th prime is $13$. |
||
| 6 | What is the $10\,001$st prime number? |
||
| 7 | |||
| 8 | ## 10001番目の素数 |
||
| 9 | 素数を小さい方から6つ並べると 2, 3, 5, 7, 11, 13 であり, 6番目の素数は 13 である. |
||
| 10 | 10001 番目の素数を求めよ. |
||
| 11 | |||
| 12 | 1 | Noppi | ```scheme |
| 13 | #!r6rs |
||
| 14 | #!chezscheme |
||
| 15 | |||
| 16 | (import (chezscheme)) |
||
| 17 | |||
| 18 | (define (prime? num) |
||
| 19 | (if (even? num) |
||
| 20 | #f |
||
| 21 | 2 | Noppi | (let ([count (isqrt num)]) |
| 22 | (let loop ([check-num 3]) |
||
| 23 | 1 | Noppi | (cond |
| 24 | [(< count check-num) #t] |
||
| 25 | [(zero? (mod num check-num)) #f] |
||
| 26 | [else (loop (+ check-num 2))]))))) |
||
| 27 | |||
| 28 | 4 | Noppi | (define (primes num) |
| 29 | 5 | Noppi | (assert (and (integer? num) |
| 30 | (exact? num) |
||
| 31 | (positive? num))) |
||
| 32 | 4 | Noppi | (let loop ([current 3] [count 1] [result '(2)]) |
| 33 | (if (= count num) |
||
| 34 | result |
||
| 35 | 1 | Noppi | (let ([next (+ current 2)]) |
| 36 | (if (prime? current) |
||
| 37 | 4 | Noppi | (loop next (add1 count) (cons current result)) |
| 38 | (loop next count result)))))) |
||
| 39 | |||
| 40 | (define answer-7 |
||
| 41 | (car (primes 10001))) |
||
| 42 | 1 | Noppi | |
| 43 | (printf "7: ~D~%" answer-7) |
||
| 44 | ``` |