プロジェクト

全般

プロフィール

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

Noppi, 2023/12/28 02:25

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
  (assert (< 0 num))
30
  (let loop ([current 3] [count 1] [result '(2)])
31
    (if (= count num)
32
      result
33 1 Noppi
      (let ([next (+ current 2)])
34
        (if (prime? current)
35 4 Noppi
          (loop next (add1 count) (cons current result))
36
          (loop next count result))))))
37
38
(define answer-7
39
  (car (primes 10001)))
40 1 Noppi
41
(printf "7: ~D~%" answer-7)
42
```