Here is a question:
By listing the first six prime
numbers: 2, 3, 5, 7, 11, and 13, we
can see that the 6th prime is 13.What is the 10001st prime number?
Here is my solution:
#lang racket
(define (search-limit num)
(+ (floor (sqrt num)) 1))
(define (search-list num)
(stream->list (in-range 2 (search-limit num))))
(define (divided? num x)
(= (remainder num x) 0))
(define (list-of-dividers num)
(filter (lambda (x)
(divided? num x))
(search-list num)))
(define (prime? num)
(= (length (list-of-dividers num)) 0))
(define (problem_7 current_prime primes counter)
(cond
[(< primes 10001)
(cond
[(prime? counter) (problem_7 counter (+ primes 1) (+ counter 1))]
[else (problem_7 current_prime primes (+ counter 1))])]
[else current_prime]))
(problem_7 0 0 0)
It works but works slowly. I am sure that there is a better solution.
Can you give me the more scheme-way solution?
I did it the following way, which takes under 1 second on my computer (your version took about 12.5 seconds):
There are certainly faster implementations of
(prime? n), but this does the trick for me.