Commits

Anonymous committed 74d4c8d

solved euler/007 in scheme

  • Participants
  • Parent commits 1021d9e

Comments (0)

Files changed (1)

euler/007-primesequence.scm

+; project euler problem 007 - find the 10001st prime
+
+(define (prime n)
+  (cond ((= n 1) 2)
+		((> n 1) (prime-iter n (list 2) 2 2 3))))
+
+(define (prime-iter n primes count last test)
+  (cond ((> count n) last) 
+		((isprime? test primes) 
+		 (prime-iter n (append primes (list test)) (+ 1 count) test (+ 2 test))) 
+		(else (prime-iter n primes count last (+ 2 test)))))
+
+(define (isprime? n primes)
+  (testprime n primes (sqrt n)))
+
+(define (testprime n primes test-max)
+  (cond ((null? primes) #t)
+		((> (car primes) test-max) #t) 
+		((not (divisible n (car primes)))
+		 (testprime n (cdr primes) test-max)) 
+		(else #f)))
+
+(define (divisible a b) 
+  (= (remainder a b) 0))
+
+(display (prime 10001)) (newline)