Numbers and Recursion CS 270 Math Foundations of
Numbers and Recursion CS 270 Math Foundations of CS Jeremy Johnson 1
Objective • To provide a recursive definition of numbers and several recursive functions for processing numbers that mimic the recursive definition • To introduce the binary representation of numbers and algorithms for performing arithmetic with binary numbers • To practice using recursive data structures and writing recursive functions in Racket 2
Outline 1. 2. 3. 4. 5. A pile of rocks Natural numbers and Peano arithmetic Binary representation of numbers Binary arithmetic Recursive definition of binary numbers
Numbers in Racket v (number? x) v integers, rationals, complex v Exact vs. inexact v Arithmetic v +, -, *, /, quotient, remainder v Comparison v <, =, <=, >, >= v Other v abs, round, gcd, sqrt, exp, log, cos, sin, …
Pile of Rocks http: //www. toonvectors. com/clip-art/cartoonillustration-of-a-caveman-staring-at-a-pile-ofrocks/123458
Pile of Rocks http: //discoverexplorelearn. com/rock-counting
Rock Numbers v (define (zero? n) (null? n)) v (define zero null) v (define one '(rock)) v (define two '(rock)) v (define three '(rock)) v (define (add 1 n) (cons 'rock n)) v (define (sub 1 n) (if (zero? n) zero (rest n))) v (define (rocknumber? n) (cond [(zero? n) #t] [(cons? n) (and (eq? (first n) 'rock) (rocknumber? (sub 1 n)))] [else #f]))
Rock Arithmetic (define (add m n) (if (zero? m) ( ))
Rock Arithmetic (define (add m n) (if (zero? m) n (add 1 (add (sub 1 m) n))))
Rock Arithmetic (define (mult m n) (if (zero? m) ( ))
Rock Arithmetic (define (mult m n) (if (zero? m) zero (add n (mult (sub 1 m) n))))
Rock Arithmetic (define (= m n) (cond [(zero? m) ] ; (and (zero? m) (zero? n)) [(zero? n) ] [else ; (or (zero? m) (zero? n)) ]))
Rock Arithmetic (define (= m n) (cond [(zero? m) (zero? n)] ; (and (zero? m) (zero? n)) [(zero? n) #f] ; (or (zero? m) (zero? n)) [else (= (sub 1 m) (sub 1 n))]))
Rock Arithmetic (define (> m n) (cond [(zero? m) ] [(zero? n) ] [else ]))
Rock Arithmetic (define (> m n) (cond [(zero? m) #f] [(zero? n) #t] [else (> (sub 1 m) (sub 1 n))]))
What is a Number? v Inductive Definition (Natural Numbers) v Number : : = zero|(succ Number) (define (zero? n) (eq? n 'zero)) (define (succ n) (list 'succ n)) (define (pred n) (if (zero? n) 'zero (second n))) (define one (succ 'zero)) (define two (succ one)) (define three (succ two)) (define (nat? x) (cond [(zero? x) #t] [(pair? x) (and (eq? (first x) 'succ) (nat? (second x)))] [else #f]))
Peano Arithmetic (define (plus m n) (if (zero? m) n (succ (plus (pred m) n)))) (define (mult m n) (if (zero? m) 'zero (plus n (mult (pred m) n)))) (define (ltnat? m n) (cond [(zero? n) #f] [(zero? m) #t] [else (ltnat? (pred m) (pred n))]))
Exercise (define (sub m n) ; m – n provide m n (if (zero? n) )) (define (div m n) ; return integer quotient (cond [(ltnat? m n) [else ] ]))
Efficient Representation v Binary numbers v B = bn-1…b 1 b 0 = bn-12 n-1 + … + b 121 + b 020 v n-bit numbers v v 2 n-1 ≤ B ≤ 2 n-1 + … + 21 + b 020 = 2 n-1 < 2 n n-1 ≤ lg(B) < n n = lg(B) + 1 Size is logarithmic What is the size of Peano or rock numbers?
Binary Addition v A = bm-1…b 1 b 0 and B = bn-1…b 1 b 0 v (define (binadd A B) ; first attempt (cond [(and (zero? A) (zero? B)) zero] [(zero? A) B] [(zero? B) A] [(cons (+ (first A) (first B)) (binadd (rest A) (rest B)))) 10010100 + 01100001 11110101
Binary Addition with Carry v A = bm-1…b 1 b 0 and B = bn-1…b 1 b 0 v (define (binadd cin A B) …) 10011100 + 01100111 100000011
Binary Multiplication v Example 10 x 7 = 70 = 26 + 22 + 2 1010 x 111 1010 _ 1000110 v [shift and add]
Recursive Binary Multiplication v Inputs: h non-negative integer a, b normalized binary numbers Output: normalized binary number = 2 h*a*b (define (binmult A B) …) v A = am-1…a 1 a 0 and B = bn-1…b 1 b 0 v a 0*B + 2*(am-1…a 1)*B [Recursion]
Efficiency v How many add 1 and sub 1 operations to add m+n using Peano Arithmetic? v Cost is O(m) v How many add operations to mult m*n? v O(m) adds with each add O(n) ops which implies cost O(mn) v Compare to binary arithmetic v For + O(max(log(m), log(n))) v For * O(log(m)*log(n))
Lab Exercise v Implement and test recursive Racket functions to add and multiply binary numbers
Inductive Definition v Bin : : = zero| (double Bin)| (double_plus 1 Bin) v Examples v 1 = (double_plus 1 zero) v 2 = (double_plus 1 zero)) v 3 = (double_plus 1 zero))
Binary Arithmetic (define one (double-plus 1 zero)) (define two (double one)) (define three (double-plus 1 one)) (define(inc b) (cond [(zero? b) (double-plus 1 b)] [(double? b) (double-plus 1 (op b))] [(double-plus 1? b) (double (inc (op b)))]))
Homework Exercise v With succ we can implement + and * using the recursive functions from Peano arithmetic; however this is inefficient. v In Assignment 2 you will implement + and * using the inductive definition of binary numbers in a more efficient way.
- Slides: 28