Commit 07b83d9a authored by bbguimaraes's avatar bbguimaraes
Browse files

sicp: section 1.2

parent 49844716
(define (factorial n)
(if (= n 1)
1
(* n (factorial (- n 1)))))
(display (factorial 6))
(newline)
(define (factorial n)
(fact-iter 1 1 n))
(define (fact-iter product counter max-count)
(if (> counter max-count)
product
(fact-iter (* counter product)
(+ counter 1)
max-count)))
(display (factorial 6))
(newline)
(define (fib n)
(cond ((= n 0) 0)
((= n 1) 1)
(else (+ (fib (- n 1))
(fib (- n 2))))))
(display (fib 5))
(newline)
(define (fib n)
(fib-iter 1 0 n))
(define (fib-iter a b count)
(if (= count 0)
b
(fib-iter (+ a b) a (- count 1))))
(display (fib 5))
(newline)
(define (count-change amount)
(cc amount 5))
(define (cc amount kinds-of-coins)
(cond ((= amount 0) 1)
((or (< amount 0) (= kinds-of-coins 0)) 0)
(else (+ (cc amount
(- kinds-of-coins 1))
(cc (- amount
(first-denomination kinds-of-coins))
kinds-of-coins)))))
(define (first-denomination kinds-of-coins)
(cond ((= kinds-of-coins 1) 1)
((= kinds-of-coins 2) 5)
((= kinds-of-coins 3) 10)
((= kinds-of-coins 4) 25)
((= kinds-of-coins 5) 50)))
(display (count-change 100))
(define (test)
(display (expt 2 1))
(display " ")
(display (expt 2 2))
(display " ")
(display (expt 2 3))
(display " ")
(display (expt 2 4))
(display " ")
(display (expt 2 5))
(display " ")
(display (expt 2 6))
(display " ")
(display (expt 2 7))
(display " ")
(display (expt 2 8))
(display " ")
(display (expt 2 9))
(display " ")
(display (expt 2 10))
(newline))
(define (expt b n)
(if (= n 0)
1
(* b (expt b (- n 1)))))
(test)
(define (expt b n)
(define (expt-iter b counter product)
(if (= counter 0)
product
(expt-iter b
(- counter 1)
(* b product))))
(expt-iter b n 1))
(test)
(define (expt b n)
(cond ((= n 0) 1)
((even? n) (square (expt b (/ n 2))))
(else (* b (expt b (- n 1))))))
(test)
(define (gcd a b)
(if (= b 0)
a
(gcd b (remainder a b))))
(display (gcd 206 40))
(newline)
(define (smallest-divisor n)
(define (find-divisor n test-divisor)
(define (divides? a b)
(= (remainder b a) 0))
(cond ((> (square test-divisor) n) n)
((divides? test-divisor n) test-divisor)
(else (find-divisor n (+ test-divisor 1)))))
(find-divisor n 2))
(define (prime? n)
(= n (smallest-divisor n)))
(define (test prime?)
(define (iter n) (display " ") (display (prime? n)))
(display "non-primes:")
(map iter '(561 1105 1729 2465 2821 6601))
(newline)
(display "primes:")
(map iter '(2 5 13 37 1009 10009)))
(test prime?)
(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 (fermat-test n)
(define (try-it a)
(= (expmod a n n) a))
(try-it (+ 1 (random (- n 1)))))
(define (fast-prime? n times)
(cond ((= times 0) true)
((fermat-test n) (fast-prime? n (- times 1)))
(else false)))
(newline)
(newline)
(test (lambda (n) (fast-prime? n 100)))
(define (A x y)
(cond ((= y 0) 0)
((= x 0) (* 2 y))
((= y 1) 2)
(else (A (- x 1)
(A x (- y 1))))))
(display (A 1 10))
(newline)
(display (A 2 4))
(newline)
(display (A 3 3))
(define (f n)
(if (< n 3)
n
(+ (f (- n 1))
(* 2 (f (- n 2)))
(* 3 (f (- n 3))))))
(display (f 5))
(newline)
(define (f n)
(if (< n 3)
n
(f-iter 0 1 2 (- n 3))))
(define (f-iter f-3 f-2 f-1 count)
(define new-f (+ f-1 (* 2 f-2) (* 3 f-3)))
(if (= count 0)
new-f
(f-iter f-2 f-1 new-f (- count 1))))
(display (f 5))
(newline)
(define (elem-xy x y)
(if (or (= x 0) (= x y))
1
(+ (elem-xy (- x 1) (- y 1))
(elem-xy x (- y 1)))))
(display (elem-xy 0 0))
(newline)
(display (elem-xy 0 1))
(display " ")
(display (elem-xy 1 1))
(newline)
(display (elem-xy 0 2))
(display " ")
(display (elem-xy 1 2))
(display " ")
(display (elem-xy 2 2))
(newline)
(display (elem-xy 0 3))
(display " ")
(display (elem-xy 1 3))
(display " ")
(display (elem-xy 2 3))
(display " ")
(display (elem-xy 3 3))
(newline)
(display (elem-xy 0 4))
(display " ")
(display (elem-xy 1 4))
(display " ")
(display (elem-xy 2 4))
(display " ")
(display (elem-xy 3 4))
(display " ")
(display (elem-xy 4 4))
(newline)
(define (expt b n)
(define (expt-iter b n a)
(cond ((= n 0) a)
((odd? n) (expt-iter b (- n 1) (* b a)))
(else (expt-iter (* b b) (/ n 2) a))))
(expt-iter b n 1))
(display (expt 2 1))
(display " ")
(display (expt 2 2))
(display " ")
(display (expt 2 3))
(display " ")
(display (expt 2 4))
(display " ")
(display (expt 2 5))
(display " ")
(display (expt 2 6))
(display " ")
(display (expt 2 7))
(display " ")
(display (expt 2 8))
(display " ")
(display (expt 2 9))
(display " ")
(display (expt 2 10))
(newline)
(define (book-* a b)
(if (= b 0)
0
(+ a (* a (- b 1)))))
(define (fast-mul a b)
(define (double x)
(+ x x))
(define (havle x)
(/ x 2))
(cond ((= b 0) 0)
((even? b) (double (fast-mul a (/ b 2))))
(else (+ a (fast-mul a (- b 1))))))
(map
(lambda (x)
(let
((book2 (book-* 2 x)) (impl2 (fast-mul 2 x))
(book3 (book-* 3 x)) (impl3 (fast-mul 3 x))
(book4 (book-* 4 x)) (impl4 (fast-mul 4 x)))
(if (< book2 10) (display " "))
(display book2)
(display " = ")
(if (< impl2 10) (display " "))
(display impl2)
(display " ")
(if (< book3 10) (display " "))
(display book3)
(display " = ")
(if (< impl3 10) (display " "))
(display impl3)
(display " ")
(if (< book4 10) (display " "))
(display book4)
(display " = ")
(if (< impl4 10) (display " "))
(display impl4)
(newline)))
'(1 2 3 4 5 6 7 8 9 10))
(define (book-* a b)
(if (= b 0)
0
(+ a (* a (- b 1)))))
(define (fast-mul a b)
(define (double x)
(+ x x))
(define (havle x)
(/ x 2))
(define (*-iter b n a)
(cond ((= n 0) a)
((odd? n) (*-iter b (- n 1) (+ b a)))
(else (*-iter (double b) (/ n 2) a))))
(*-iter b a 0))
(map
(lambda (x)
(let
((book2 (book-* 2 x)) (impl2 (fast-mul 2 x))
(book3 (book-* 3 x)) (impl3 (fast-mul 3 x))
(book4 (book-* 4 x)) (impl4 (fast-mul 4 x)))
(if (< book2 10) (display " "))
(display book2)
(display " = ")
(if (< impl2 10) (display " "))
(display impl2)
(display " ")
(if (< book3 10) (display " "))
(display book3)
(display " = ")
(if (< impl3 10) (display " "))
(display impl3)
(display " ")
(if (< book4 10) (display " "))
(display book4)
(display " = ")
(if (< impl4 10) (display " "))
(display impl4)
(newline)))
'(1 2 3 4 5 6 7 8 9 10))
(define (fib n)
(fib-iter 1 0 0 1 n))
(define (fib-iter a b p q count)
(cond ((= count 0) b)
((even? count)
(fib-iter a
b
(+ (* p p) (* q q))
(+ (* q q) (* 2 p q))
(/ count 2)))
(else (fib-iter (+ (* b q) (* a q) (* a p))
(+ (* b p) (* a q))
p
q
(- count 1)))))
(display (fib 0))
(newline)
(display (fib 1))
(newline)
(display (fib 2))
(newline)
(display (fib 3))
(newline)
(display (fib 4))
(newline)
(display (fib 5))
(newline)
(display (fib 6))
(newline)
(display (fib 7))
(newline)
(display (fib 8))
(define (smallest-divisor n)
(define (find-divisor n test-divisor)
(define (divides? a b)
(= (remainder b a) 0))
(cond ((> (square test-divisor) n) n)
((divides? test-divisor n) test-divisor)
(else (find-divisor n (+ test-divisor 1)))))
(find-divisor n 2))
(display (smallest-divisor 199))
(newline)
(display (smallest-divisor 1999))
(newline)
(display (smallest-divisor 19999))
(newline)
(define (smallest-divisor n)
(define (find-divisor n test-divisor)
(define (divides? a b)
(= (remainder b a) 0))
(cond ((> (square test-divisor) n) n)
((divides? test-divisor n) test-divisor)
(else (find-divisor n (+ test-divisor 1)))))
(find-divisor n 2))
(define (prime? n)
(= n (smallest-divisor n)))
(define (timed-prime-test n)
(newline)
(display n)
(start-prime-test n (runtime)))
(define (start-prime-test n start-time)
(if (prime? n)
(report-prime (- (runtime) start-time))))
(define (report-prime elapsed-time)
(display " *** ")
(display elapsed-time))
(define (search-for-primes start end)
(define (search-for-primes-iter i)
(cond ((<= i end)
(timed-prime-test i)
(search-for-primes-iter (+ i 1)))))
(search-for-primes-iter start))
(search-for-primes 3 1000)
(define (smallest-divisor n)
(define (find-divisor n test-divisor)
(define (divides? a b)
(= (remainder b a) 0))
(cond ((> (square test-divisor) n) n)
((divides? test-divisor n) test-divisor)
(else (find-divisor n (next test-divisor)))))
(find-divisor n 2))
(define (next x)
(if (= x 2) 3 (+ x 2)))
(define (prime? n)
(= n (smallest-divisor n)))
(define (timed-prime-test n)
(newline)
(display n)
(start-prime-test n (runtime)))
(define (start-prime-test n start-time)
(if (prime? n)
(report-prime (- (runtime) start-time))))
(define (report-prime elapsed-time)
(display " *** ")
(display elapsed-time))
(define (search-for-primes start end)
(define (search-for-primes-iter i)
(cond ((<= i end)
(timed-prime-test i)
(search-for-primes-iter (+ i 1)))))
(search-for-primes-iter start))
(search-for-primes 3 1000)
(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 (fermat-test n)
(define (try-it a)
(= (expmod a n n) a))
(try-it (+ 1 (random (- n 1)))))
(define (fast-prime? n times)
(cond ((= times 0) true)
((fermat-test n) (fast-prime? n (- times 1)))
(else false)))
(define (timed-prime-test n)
(newline)
(display n)
(start-prime-test n (runtime)))
(define (start-prime-test n start-time)
(if (fast-prime? n 100)
(report-prime (- (runtime) start-time))))
(define (report-prime elapsed-time)
(display " *** ")
(display elapsed-time))
(define (search-for-primes start end)
(define (search-for-primes-iter i)
(cond ((<= i end)
(timed-prime-test i)
(search-for-primes-iter (+ i 1)))))
(search-for-primes-iter start))
(search-for-primes 3 1000)
(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 (fermat-test n)
(define (try-it a)
(= (expmod a n n) a))
(try-it (+ 1 (random (- n 1)))))
(define (test-charmichael n)
(define (test-charmichael-iter i)
(cond ((= i n) true)
((fermat-test n) (test-charmichael-iter (+ i 1)))
(else false)))
(test-charmichael-iter 0))
(display (test-charmichael 561))
(newline)
(display (test-charmichael 1105))
(newline)
(display (test-charmichael 1729))
(newline)
(display (test-charmichael 2465))
(newline)
(display (test-charmichael 2821))
(newline)
(display (test-charmichael 6601))
(newline)
(define (expmod base exp m)
(define (squaremod-with-check x)
(define (check-nontrivial x s)
(if (and (= s 1) (not (= x 1)) (not (= x (- m 1))))
0 s))
(check-nontrivial x (remainder (square x) m)))
(cond ((= exp 0) 1)
((even? exp) (squaremod-with-check (expmod base (/ exp 2) m)))
(else (remainder (* base (expmod base (- exp 1) m)) m))))
(define (miller-rabin-test n)
(define (try-it a)
(define (test-expmod x)
(and (not (= x 0)) (= x 1)))
(test-expmod (expmod a (- n 1) n)))
(try-it (+ 1 (random (- n 1)))))
(define (fast-prime? n)
(define (test-prime-iter i)
(cond ((= i 0) true)
((miller-rabin-test n) (test-prime-iter (- i 1)))
(else false)))
(test-prime-iter 100))
(display "non-primes: ")
(display (fast-prime? 561))
(display " ")
(display (fast-prime? 1105))
(display " ")
(display (fast-prime? 1729))
(display " ")
(display (fast-prime? 2465))
(display " ")
(display (fast-prime? 2821))
(display " ")
(display (fast-prime? 6601))
(newline)
(display "primes: ")
(display (fast-prime? 2))
(display " ")
(display (fast-prime? 5))
(display " ")
(display (fast-prime? 13))
(display " ")
(display (fast-prime? 37))
(display " ")
(display (fast-prime? 1009))
(display " ")
(display (fast-prime? 10009))
(newline)
(define (inc x) (+ x 1))
(define (dec x) (- x 1))
(define (add a b)
(if (= a 0)
b
(inc (add (dec a) b))))
(display (add 2 7))
(define (add a b)
(if (= a 0)
b
(add (dec a) (inc b))))
(newline)
(display (add 2 7))