1 / 28

Numbers and Recursion

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.

altman
Download Presentation

Numbers and Recursion

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Numbers and Recursion CS 270 Math Foundations of CS Jeremy Johnson

  2. 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

  3. Outline • A pile of rocks • Natural numbers and Peano arithmetic • Binary representation of numbers • Binary arithmetic • Recursive definition of binary numbers

  4. Numbers in Racket • (number? x) • integers, rationals, complex • Exact vs. inexact • Arithmetic • +,-,*,/,quotient, remainder • Comparison • <,=,<=,>,>= • Other • abs, round, gcd, sqrt, exp, log, cos, sin, …

  5. Pile of Rocks http://www.toonvectors.com/clip-art/cartoon-illustration-of-a-caveman-staring-at-a-pile-of-rocks/123458

  6. Pile of Rocks http://discoverexplorelearn.com/rock-counting

  7. 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]))

  8. Rock Arithmetic • (define (add m n) • (if (zero? m) • ( ))

  9. Rock Arithmetic • (define (add m n) • (if (zero? m) • n • (add1 (add (sub1 m) n))))

  10. Rock Arithmetic • (define (mult m n) • (if (zero? m) • ( ))

  11. Rock Arithmetic • (define (mult m n) • (if (zero? m) • zero • (add n (mult (sub1 m) n))))

  12. Rock Arithmetic • (define (= m n) • (cond • [(zero? m) ] ; (and (zero? m) (zero? n)) • [(zero? n) ] ; (or (zero? m) (zero? n)) • [else ]))

  13. 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))]))

  14. Rock Arithmetic • (define (> m n) • (cond • [(zero? m) ] • [(zero? n) ] • [else ]))

  15. Rock Arithmetic • (define (> m n) • (cond • [(zero? m) #f] • [(zero? n) #t] • [else (> (sub1 m) (sub1 n))]))

  16. 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]))

  17. 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))]))

  18. 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 ]))

  19. 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?

  20. 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

  21. Binary Addition with Carry • A= bm-1…b1b0and B = bn-1…b1b0 • (define (binaddcin A B) • …) • 10011100 • + 01100111 • 100000011

  22. Binary Multiplication • Example 10 x 7 = 70 = 26 + 22 + 2 • 1010 • x 111 • 1010 • 1010 [shift and add] • 1010 _ [shift and add] • 1000110

  23. 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]

  24. 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))

  25. Lab Exercise • Implement and test recursive Racket functions to add and multiply binary numbers

  26. 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))

  27. 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)))]))

  28. 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.

More Related