diff options
author | David Thompson <dthompson2@worcester.edu> | 2014-02-02 15:07:46 -0500 |
---|---|---|
committer | David Thompson <dthompson2@worcester.edu> | 2014-02-02 15:07:46 -0500 |
commit | f211fe46db8eba59efbdefad8fe2ec2861921bae (patch) | |
tree | 170429d74afcc04957b27e9c6f4edeed38255b27 | |
parent | c24e06d026e6d75a5f3d8c0522c64d07301caae8 (diff) |
Solve Problem 3.
-rw-r--r-- | problem-3.scm | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/problem-3.scm b/problem-3.scm new file mode 100644 index 0000000..9659975 --- /dev/null +++ b/problem-3.scm @@ -0,0 +1,38 @@ +(use-modules (srfi srfi-1)) + +(define (prime-sieve n) + (define (mark-multiples! bits x) + (define (mark y) + (when (< y n) + (bitvector-set! bits y #t) + (mark (+ y y)))) + (mark x)) + (define (sieve m primes bits) + (cond ((= m n) + primes) + ((bitvector-ref bits m) + (sieve (1+ m) primes bits)) + (else + (mark-multiples! bits m) + (sieve (1+ m) (cons m primes) bits)))) + (reverse (sieve 2 '() (make-bitvector n)))) + +(define (divisible? m n) + (zero? (modulo m n))) + +(define (integer x) + (inexact->exact (floor x))) + +(define (factors n) + (define (factor n primes factors) + (cond ((null? primes) + factors) + ((divisible? n (car primes)) + (factor (/ n (car primes)) + (cdr primes) + (cons (car primes) factors))) + (else + (factor n (cdr primes) factors)))) + (factor n (prime-sieve (integer (1+ (sqrt n)))) '())) + +(car (factors 600851475143)) |