
Recent Posts
Archives
Categories
Monthly Archives: January 2012
SICP solution exercise 1.29
Solution to exercise 1.29 of SICP. (define (cube x) (* x x x)) (define (sum term a next b) (if (> a b) 0 (+ (term a) (sum term (next a) next b)))) (define (simpson f a b n) (define … Continue reading
SICP solution exercise 1.26
Solution to exercise 1.26 of SICP. Louis’ code generates a treerecursive process for expmod, like the one in the Fibonacci example, which grows exponentially. Louis’ fastprime? test altered the fast original fastprime? test from Θ(log2 n) to Θ(log22n) which is … Continue reading
SICP solution exercise 1.25
Compare the execution times with the original expmod and Alyssa’s alternative expmod. Results in seconds: Original expmod method: 1 ]=> (searchforprimes 10000 10500) 10007 *** .01 10009 *** 9.999999999999998e3 10037 *** 1.0000000000000002e2 Alyssa’s expmod method: 1 ]=> (searchforprimes 10000 10500) … Continue reading
SICP solution exercise 1.24
Solution to exercise 1.24 of SICP (define (expmod base exp m) (cond ((= exp 0) 1) ((even? exp) (remainder (square (expmod base (/ exp 2) m)) m)) (else (remainder (* base (expmod base ( exp 1) m)) m)))) (define (timedprimetest … Continue reading
SICP solution exercise 1.23
Solution to exercise 1.23 of SICP (define (smallestdivisor n) (finddivisor n 2)) (define (finddivisor n testdivisor) (define (next d) (if (= d 2) 3 (+ d 2))) (cond ((> (square testdivisor) n) n) ((divides? testdivisor n) testdivisor) (else (finddivisor n … Continue reading
SICP solution exercise 1.22
Solution for exercise 1.22 of SICP (define (smallestdivisor n) (finddivisor n 2)) (define (finddivisor n testdivisor) (cond ((> (square testdivisor) n) n) ((divides? testdivisor n) testdivisor) (else (finddivisor n (+ testdivisor 1))))) (define (divides? a b) (= (remainder b a) … Continue reading
SICP solution exercise 1.21
Solution for exercise 1.21 of SICP (smallestdivisor 199) ;Value: 199 (smallestdivisor 1999) ;Value: 1999 (smallestdivisor 19999) ;Value: 7