;; This file contains excerpts from the textbook Concrete ;; Abstractions: An Introduction to Computer Science Using Scheme, by ;; Max Hailperin, Barbara Kaiser, and Karl Knight, Copyright (c) 1998 ;; by the authors. Full text is available for free at ;; http://www.gustavus.edu/+max/concrete-abstractions.html ;; Chapter 4: Orders of Growth and Tree Recursion ;; 4.1 Orders of Growth ;; 4.2 Tree Recursion and Digital Signatures (define verify (lambda (signature modulus) (remainder (expt signature 3) modulus))) (define gold-num 5972304273877744135569338397692020533504) (define signature 143676221783307728140118556730532825709962359695147398872633033728948225540940112091576952965868445265161373616153020167902900930324840824269164789456142215776895016041636987254848119449940440885630) (define modulus 671629488048603400615365258174985654900765971941961654084193604750896012182890124354255484422321487634816640987992317596893099956961956383454333339584850276505584537663630293912940840460009374858969) (verify signature modulus) ;Value: 5972304273877744135569338397692020533504 (= gold-num (verify signature modulus)) ;Value: #t (define mod+ (lambda (x y) (remainder (+ x y) modulus))) (define mod* (lambda (x y) (remainder (* x y) modulus))) (define mod- (lambda (x y) (remainder (+ x (- modulus y)) modulus))) (define signing-exponent 447752992032402267076910172116657103267177314627974436056129069833930674788593416236170322948214322483305175278012793102392215895931470577163544613600143471679799876664686423606429437389098641670667) (remainder (expt gold-num signing-exponent) modulus) (define mod-expt (lambda (base exponent modulus) (define mod* (lambda (x y) (remainder (* x y) modulus))) (if (= exponent 0) 1 (mod* (mod-expt base (- exponent 1) modulus) base)))) (mod-expt signature 3 modulus) ;Value: 5972304273877744135569338397692020533504 (mod-expt 7 signing-exponent modulus) ;Aborting!: out of memory (define mod-expt (lambda (base exponent modulus) (define mod* (lambda (x y) (remainder (* x y) modulus))) (if (= exponent 0) 1 (if (even? exponent) (mod* (mod-expt base (/ exponent 2) modulus) (mod-expt base (/ exponent 2) modulus)) (mod* (mod-expt base (- exponent 1) modulus) base))))) (define mod-expt (lambda (base exponent modulus) (define mod* (lambda (x y) (remainder (* x y) modulus))) (if (= exponent 0) 1 (if (even? exponent) (let ((x (mod-expt base (/ exponent 2) modulus))) (mod* x x)) (mod* (mod-expt base (- exponent 1) modulus) base))))) ;; 4.3 An Application: Fractal Curves (define c-curve (lambda (x0 y0 x1 y1 level) (if (= level 0) (line x0 y0 x1 y1) (let ((xmid (/ (+ x0 x1) 2)) (ymid (/ (+ y0 y1) 2)) (dx (- x1 x0)) (dy (- y1 y0))) (let ((xa (- xmid (/ dy 2))) (ya (+ ymid (/ dx 2)))) (overlay (c-curve x0 y0 xa ya (- level 1)) (c-curve xa ya x1 y1 (- level 1)))))))) (c-curve 0 -1/2 0 1/2 0) (c-curve 0 -1/2 0 1/2 1) (c-curve 0 -1/2 0 1/2 2) (c-curve 0 -1/2 0 1/2 3) (c-curve 0 -1/2 0 1/2 4) (c-curve -1/2 0 0 1/2 3) (c-curve 0 -1/2 -1/2 0 3) (overlay (c-curve -1/2 0 0 1/2 3) (c-curve 0 -1/2 -1/2 0 3)) (c-curve 0 -1/2 0 1/2 6) (c-curve 0 -1/2 0 1/2 10) (c-curve 0 0 1/2 1/2 0) (c-curve 0 0 1/2 1/2 4) (c-curve 1/2 1/2 0 0 4) (length-of-c-curve 0 -1/2 0 1/2 0) (length-of-c-curve 0 -1/2 0 1/2 1) (length-of-c-curve 0 -1/2 0 1/2 2) (length-of-c-curve 0 -1/2 0 1/2 3) (length-of-c-curve 0 -1/2 0 1/2 4) ;; Review Problems (define factorial (lambda (n) (if (= n 0) 1 (* n (factorial (- n 1)))))) (define factorial-sum1 ; returns 1! + 2! + ... + n! (lambda (n) (if (= n 0) 0 (+ (factorial n) (factorial-sum1 (- n 1)))))) (define factorial-sum2 ; also returns 1! + 2! + ... + n! (lambda (n) (define loop (lambda (k fact-k addend) (if (> k n) addend (loop (+ k 1) (* fact-k (+ k 1)) (+ addend fact-k))))) (loop 1 1 0))) (define ways-to-factor (lambda (n) (ways-to-factor-using-no-smaller-than n 2))) (define bar (lambda (n) (cond ((= n 0) 5) ((= n 1) 7) (else (* n (bar (- n 2))))))) (define foo (lambda (n) ; computes n! + (n!)^n (+ (factorial n) ; that is, (n! plus n! to the nth power) (bar n n)))) (define bar (lambda (i j) ; computes (i!)^j (i! to the jth power) (if (= j 0) 1 (* (factorial i) (bar i (- j 1)))))) (define factorial (lambda (n) (if (= n 0) 1 (* n (factorial (- n 1)))))) (min 1 3) (min 2 -3) (min 4 4) ;Value: 1 ;Value: -3 ;Value: 4 (define foo (lambda (low high level) (let ((mid (/ (+ low high) 2))) (let ((mid-line (line mid 0 mid (* level .1)))) (if (= level 1) mid-line (overlay mid-line (overlay (foo low mid (- level 1)) (foo mid high (- level 1)))))))))