プロジェクト

全般

プロフィール

Problem 7 » 履歴 » バージョン 3

Noppi, 2023/12/27 13:21

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
(define answer-7
29 2 Noppi
  (let loop ([current 3] [count 1] [prime 2])
30 1 Noppi
    (if
31
      (= 10001 count)
32
      prime
33 2 Noppi
      (let ([next (+ current 2)])
34 1 Noppi
        (if (prime? current)
35
          (loop next (add1 count) current)
36
          (loop next count prime))))))
37
38
(printf "7: ~D~%" answer-7)
39
```