SUNY Geneseo Department of Computer Science


Induction and Recursion

{Date}

CSci 141, Fall 2003
Prof. Doug Baldwin

Return to List of Lectures

Previous Lecture


Questions?

0! = 1 because that's how mathematicians define it

Reading

Section 7.1.2

Induction proofs, how to use them to prove algorithms correct

e.g., induction on actual values from method,

A bunch of different ways of using induction

Strong induction vs weak induction

Examples

How would you prove that our powerOf2 algorithm correctly computes 2n for any natural number n?

    int powerOf2( int n )
        if ( n > 0 )
            return 2 * powerOf2( n-1 )
        else
            return 1

Prove that this computes sum of the squares of the naturals between 0 and n

    sumSquares( n )
        if n > 0
            return n*n + sumSquare( n-1 )
        else
            return 0

Prove that this algorithm determines whether natural number n is odd or even:

    isOdd( n )
        if ( n == 0 )
            return false
        else if ( n == 1 )
            return true
        else 
            return isOdd( n - 2 )

Next Lecture