Orders of Growth

> 
(define (expt-1 b n)
  (if (= n 0)
      1
      (* b (expt-1 b (- n 1)))))

expt-1

> 
(expt-1 1 0)

1

> 
(expt-1 2 3)

8

> 
(define (expt-2 b n)
  (define (expt-iter b counter product)
    (if (= counter 0)
        product
        (expt-iter b
                  (- counter 1)
                  (* b product))))
  
  (expt-iter b n 1))

expt-2

> 
(expt-2 3 3)

27

> 

Exponentiation

> 
(define (fast-expt b n)
  (define (even? n)
    (= (remainder n 2) 0))
  (define (square x) (* x x))
  
  (cond ((= n 0) 1)
        ((even? n) (square (fast-expt b (/ n 2))))
        (else (* b (fast-expt b (- n 1))))))

fast-expt

> 
(fast-expt 3 3)

27

> 

Greatest Common Divisors

> 
(define (gcd a b)
  (if (= b 0)
      a
      (gcd b (remainder a b))))

gcd

> 
(gcd 10 20)

10

>