recursion and induction reasoning about recursive programs
play

Recursion and Induction: Reasoning About Recursive Programs Greg - PowerPoint PPT Presentation

Recursion and Induction: Reasoning About Recursive Programs Greg Plaxton Theory in Programming Practice, Spring 2005 Department of Computer Science University of Texas at Austin Proving Properties of power2 Recall the recursive program


  1. Recursion and Induction: Reasoning About Recursive Programs Greg Plaxton Theory in Programming Practice, Spring 2005 Department of Computer Science University of Texas at Austin

  2. Proving Properties of power2 • Recall the recursive program power2 presented last time power2 0 = 1 power2 (n+1) = 2 * (power2 n) • Claim: For all nonnegative integers n , the value of the power2 function applied to n is 2 n • While this claim may seem relatively obvious, how can we formally prove it? Theory in Programming Practice, Plaxton, Spring 2005

  3. Proving Properties of count • Recall the recursive program count we presented last time count 0 = 0 count n | even n = count (n ‘div‘ 2) | odd n = count (n ‘div‘ 2) + 1 • For any nonnegative integer n , let C ( n ) denote the value returned by the count function applied to argument n • Claim 1: For all nonnegative integers n , C ( n ) equals the number of 1s in the binary representation of n Theory in Programming Practice, Plaxton, Spring 2005

  4. Proving Properties of count • Claim 2: For all nonnegative integers m and n , C ( m + n ) ≤ C ( m ) + C ( n ) • Hint: Use induction over pairs ( m, n ) (ordered lexicographically, say), and consider cases depending on the parity of m and n Theory in Programming Practice, Plaxton, Spring 2005

  5. Proving Properties of fib • Recall the recursive program we presented last time for computing the Fibonacci numbers fib 0 = 0 fib 1 = 1 fib (n + 2) = (fib n) + (fib (n+1)) • For any nonnegative integer n , let F ( n ) denote the value returned by the fib function applied to argument n • Claim: For all integers m and n such that m > 0 and n ≥ 0 F ( m + n ) = F ( m − 1) · F ( n ) + F ( m ) · F ( n + 1) – Hint: Fix m arbitrarily and use induction on n alone Theory in Programming Practice, Plaxton, Spring 2005

  6. Proving Properties of fib and gcd • Recall Euclid’s gcd algorithm gcd m n | m == n = m | m > n = gcd (m - n) n | n > m = gcd m (n - m) • For any nonnegative integers m and n , let g ( m, n ) denote the value returned by the gcd function applied to arguments m and n • Claim: For all positive integers n g ( F ( n ) , F ( n + 1)) = 1 Theory in Programming Practice, Plaxton, Spring 2005

Recommend


More recommend