Mercurial > hg > jslab
view src/scheme/weird.scm @ 0:bf79fb79ee13
Initial Mercurial check in.
author | samer |
---|---|
date | Tue, 17 Jan 2012 17:50:20 +0000 |
parents | |
children |
line wrap: on
line source
(define (sieve n pr) (if (divides n pr) pr (cons n pr))) (define (div n m) (equal? 0 (remainder n m))) (define (divides n factors) (if (null? factors) #f (if (div n (first factors)) #t (divides n (rest factors))))) (define primeso (let ((hashed (java.util.ArrayList.))) (define (set n l) (.add hashed n l) l) (.add hashed `()) (.add hashed `()) (.add hashed `(2)) (lambda (n) (if (< n (.size hashed)) (.get hashed n) (begin (.ensureCapacity hashed (+ n 1)) (set n (sieve n (primes (- n 1))))))))) (define (accum-primes n sofar limit) (if (> n limit) sofar (accum-primes (+ n 1) (sieve n sofar) limit))) (define (primes limit) (accum-primes 2 () limit)) ; alternative is to make a big or list by mapping ; factors to a list of procedures: (m1 m2 ..) -> ((div m1) (div m2) ...) (define (prime? n) (equal? n (first (primes n)))) (define (prime-factors n) `(define (factors n) (define prime-factors (prime-factors n)) ; now make all products including 1 but excluding n ; this is a binary enumeration, basically ) (define (proper-divisors n) (define (abundant? n) (< n (apply + (factors n)))) (define (weird? n) ; get factors ; check abundancy ; is n a sum of any of its factors? (define sum (apply + proper-divisors)) (if (<= sum n) #f ; ie not abundant (begin (define discrep (- n sum)) ; can we make discrep by summing divisors <= discrep ) ) )