280 likes | 293 Views
This resource provides a recursive definition of numbers and explores the concept of binary representation of numbers. It also includes recursive functions for performing arithmetic operations with binary numbers.
E N D
Numbers and Recursion CS 270 Math Foundations of CS Jeremy Johnson
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
Outline • A pile of rocks • Natural numbers and Peano arithmetic • Binary representation of numbers • Binary arithmetic • Recursive definition of binary numbers
Numbers in Racket • (number? x) • integers, rationals, complex • Exact vs. inexact • Arithmetic • +,-,*,/,quotient, remainder • Comparison • <,=,<=,>,>= • Other • abs, round, gcd, sqrt, exp, log, cos, sin, …
Pile of Rocks http://www.toonvectors.com/clip-art/cartoon-illustration-of-a-caveman-staring-at-a-pile-of-rocks/123458
Pile of Rocks http://discoverexplorelearn.com/rock-counting
Rock Numbers • (define (zero? n) (null? n)) • (define zero null) • (define one '(rock)) • (define two '(rock rock)) • (define three '(rock rock rock)) • (define (add1 n) (cons 'rock n)) • (define (sub1 n) (if (zero? n) zero (rest n))) • (define (rocknumber? n) • (cond • [(zero? n) #t] • [(cons? n) (and (eq? (first n) 'rock) (rocknumber? (sub1 n)))] • [else #f]))
Rock Arithmetic • (define (add m n) • (if (zero? m) • ( ))
Rock Arithmetic • (define (add m n) • (if (zero? m) • n • (add1 (add (sub1 m) n))))
Rock Arithmetic • (define (mult m n) • (if (zero? m) • ( ))
Rock Arithmetic • (define (mult m n) • (if (zero? m) • zero • (add n (mult (sub1 m) n))))
Rock Arithmetic • (define (= m n) • (cond • [(zero? m) ] ; (and (zero? m) (zero? n)) • [(zero? n) ] ; (or (zero? m) (zero? n)) • [else ]))
Rock Arithmetic • (define (= m n) • (cond • [(zero? m) (zero? n)] ; (and (zero? m) (zero? n)) • [(zero? n) #f] ; (or (zero? m) (zero? n)) • [else (= (sub1 m) (sub1 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 (> (sub1 m) (sub1 n))]))
What is a Number? • Inductive Definition (Natural Numbers) • 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 (nat? x) • (define two (succ one)) (cond • (define three (succ two)) [(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 • Binary numbers • B = bn-1…b1b0 = bn-12n-1 + … + b121 + b020 • n-bit numbers • 2n-1≤ B ≤ 2n-1 + … + 21 + b020= 2n-1 < 2n • n-1 ≤ lg(B) < n n = lg(B) + 1 • Size is logarithmic • What is the size of Peano or rock numbers?
Binary Addition • A= bm-1…b1b0and B = bn-1…b1b0 • (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 • A= bm-1…b1b0and B = bn-1…b1b0 • (define (binaddcin A B) • …) • 10011100 • + 01100111 • 100000011
Binary Multiplication • Example 10 x 7 = 70 = 26 + 22 + 2 • 1010 • x 111 • 1010 • 1010 [shift and add] • 1010 _ [shift and add] • 1000110
Recursive Binary Multiplication • Inputs: h non-negative integer • a,b normalized binary numbers • Output: normalized binary number = 2h*a*b • (define (binmultA B) • …) • A = am-1…a1a0 and B = bn-1…b1b0 • a0*B + 2*(am-1…a1)*B[Recursion]
Efficiency • How many add1 and sub1 operations to add m+n using Peano Arithmetic? • Cost is O(m) • How many add operations to mult m*n? • O(m) adds with each add O(n) ops which implies cost O(mn) • Compare to binary arithmetic • For + O(max(log(m),log(n))) • For * O(log(m)*log(n))
Lab Exercise • Implement and test recursive Racket functions to add and multiply binary numbers
Inductive Definition • Bin ::= zero| • (double Bin)| • (double_plus1 Bin) • Examples • 1 = (double_plus1 zero) • 2 = (double (double_plus1 zero)) • 3 = (double_plus1 (double_plus1 zero))
Binary Arithmetic • (define one (double-plus1 zero)) • (define two (double one)) • (define three (double-plus1 one)) • (define(inc b) • (cond • [(zero? b) (double-plus1 b)] • [(double? b) (double-plus1 (op b))] • [(double-plus1? b) (double (inc(op b)))]))
Homework Exercise • With succ we can implement + and * using the recursive functions from Peano arithmetic; however this is inefficient. • In Assignment 2 you will implement + and * using the inductive definition of binary numbers in a more efficient way.