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

Posted in SICP | Tagged , | Leave a comment

SICP solution exercise 1.26

Solution to exercise 1.26 of SICP. Louis’ code generates a tree-recursive process for expmod, like the one in the Fibonacci example, which grows exponentially. Louis’ fast-prime? test altered the fast original fast-prime? test from Θ(log2 n) to Θ(log22n) which is … Continue reading

Posted in SICP | Tagged , | Leave a comment

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 ]=> (search-for-primes 10000 10500) 10007 *** .01 10009 *** 9.999999999999998e-3 10037 *** 1.0000000000000002e-2 Alyssa’s expmod method: 1 ]=> (search-for-primes 10000 10500) … Continue reading

Posted in SICP | Tagged , | Leave a comment

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 (timed-prime-test … Continue reading

Posted in SICP | Tagged , | Leave a comment

SICP solution exercise 1.23

Solution to exercise 1.23 of SICP (define (smallest-divisor n) (find-divisor n 2)) (define (find-divisor n test-divisor) (define (next d) (if (= d 2) 3 (+ d 2))) (cond ((> (square test-divisor) n) n) ((divides? test-divisor n) test-divisor) (else (find-divisor n … Continue reading

Posted in SICP | Tagged , | Leave a comment

SICP solution exercise 1.22

Solution for exercise 1.22 of SICP (define (smallest-divisor n) (find-divisor n 2)) (define (find-divisor n test-divisor) (cond ((> (square test-divisor) n) n) ((divides? test-divisor n) test-divisor) (else (find-divisor n (+ test-divisor 1))))) (define (divides? a b) (= (remainder b a) … Continue reading

Posted in SICP | Tagged , | Leave a comment

SICP solution exercise 1.21

Solution for exercise 1.21 of SICP (smallest-divisor 199) ;Value: 199 (smallest-divisor 1999) ;Value: 1999 (smallest-divisor 19999) ;Value: 7

Posted in SICP | Tagged , | Leave a comment