> |
(define (expt-1 b n) (if (= n 0) 1 (* b (expt-1 b (- n 1))))) |
> |
(expt-1 1 0) |
> |
(expt-1 2 3) |
> |
(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 3 3) |
> |
> |
(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 3 3) |
> |
> |
(define (gcd a b) (if (= b 0) a (gcd b (remainder a b)))) |
> |
(gcd 10 20) |
> |