プロジェクト

全般

プロフィール

Problem 7 » 履歴 » リビジョン 4

リビジョン 3 (Noppi, 2023/12/27 13:21) → リビジョン 4/5 (Noppi, 2023/12/28 02:25)

[ホーム](https://redmine.noppi.jp) - [[Wiki|Project Euler]] 
 # [[Problem 7]] 

 ## $10001$st Prime 
 By listing the first six prime numbers: $2, 3, 5, 7, 11$, and $13$, we can see that the $6$th prime is $13$. 
 What is the $10\,001$st prime number? 

 ## 10001番目の素数 
 素数を小さい方から6つ並べると 2, 3, 5, 7, 11, 13 であり, 6番目の素数は 13 である. 
 10001 番目の素数を求めよ. 

 ```scheme 
 #!r6rs 
 #!chezscheme 

 (import (chezscheme)) 

 (define (prime? num) 
   (if (even? num) 
     #f 
     (let ([count (isqrt num)]) 
       (let loop ([check-num 3]) 
         (cond 
           [(< count check-num) #t] 
           [(zero? (mod num check-num)) #f] 
           [else (loop (+ check-num 2))]))))) 

 (define (primes num) answer-7 
   (assert (< 0 num)) 
   (let loop ([current 3] [count 1] [result '(2)]) [prime 2]) 
     (if 
       (= count num) 10001 count) 
       result prime 
       (let ([next (+ current 2)]) 
         (if (prime? current) 
           (loop next (add1 count) (cons current result)) current) 
           (loop next count result)))))) 

 (define answer-7 
   (car (primes 10001))) prime)))))) 

 (printf "7: ~D~%" answer-7) 
 ```