Problem 7 » 履歴 » バージョン 2
Noppi, 2023/12/27 04:49
1 | 1 | Noppi | [ホーム](https://redmine.noppi.jp) - [[Wiki|Project Euler]] |
---|---|---|---|
2 | # [[Problem 7]] |
||
3 | |||
4 | ```scheme |
||
5 | #!r6rs |
||
6 | #!chezscheme |
||
7 | |||
8 | (import (chezscheme)) |
||
9 | |||
10 | (define (prime? num) |
||
11 | (if (even? num) |
||
12 | #f |
||
13 | 2 | Noppi | (let ([count (isqrt num)]) |
14 | (let loop ([check-num 3]) |
||
15 | 1 | Noppi | (cond |
16 | [(< count check-num) #t] |
||
17 | [(zero? (mod num check-num)) #f] |
||
18 | [else (loop (+ check-num 2))]))))) |
||
19 | |||
20 | (define answer-7 |
||
21 | 2 | Noppi | (let loop ([current 3] [count 1] [prime 2]) |
22 | 1 | Noppi | (if |
23 | (= 10001 count) |
||
24 | prime |
||
25 | 2 | Noppi | (let ([next (+ current 2)]) |
26 | 1 | Noppi | (if (prime? current) |
27 | (loop next (add1 count) current) |
||
28 | (loop next count prime)))))) |
||
29 | |||
30 | (printf "7: ~D~%" answer-7) |
||
31 | ``` |