Created
November 5, 2014 06:53
-
-
Save tgallant/9586058191d5c0b4af01 to your computer and use it in GitHub Desktop.
euler7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; helpers | |
(define (nth n l) | |
(if (or (> n (length l)) (< n 0)) | |
(error "Index out of bounds.") | |
(if (eq? n 0) | |
(car l) | |
(nth (- n 1) (cdr l))))) | |
(define (sieve n) | |
(define (aux u v) | |
(let ((p (car v))) | |
(if (> (* p p) n) | |
(let rev-append ((u u) (v v)) | |
(if (null? u) v (rev-append (cdr u) (cons (car u) v)))) | |
(aux (cons p u) | |
(let wheel ((u '()) (v (cdr v)) (a (* p p))) | |
(cond ((null? v) (reverse u)) | |
((= (car v) a) (wheel u (cdr v) (+ a p))) | |
((> (car v) a) (wheel u v (+ a p))) | |
(else (wheel (cons (car v) u) (cdr v) a)))))))) | |
(aux '(2) | |
(let range ((v '()) (k (if (odd? n) n (- n 1)))) | |
(if (< k 3) v (range (cons k v) (- k 2)))))) | |
;; e7 | |
(nth 10000 (sieve 110000)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment