L22cs2110fa09-6up

# L22cs2110fa09-6up - 11/12/2009 Overview: Reasoning about...

This preview shows pages 1–3. Sign up to view the full content.

11/12/2009 1 A well-known scientist (some say it was Bertrand Russell) once gave a public lecture on astronomy. He described how the earth orbits around the sun and how the sun, in turn, orbits around the center of a vast collection of stars called our galaxy. At the end of the lecture, a little old lady at the back of the room got up and INDUCTION Lecture 22 CS2110 – Fall 2009 said: "What you have told us is rubbish. The world is really a flat plate supported on the back of a giant tortoise." The scientist gave a superior smile before replying, "What is the tortoise standing on?" "You're very clever, young man, very clever", said the old lady. "But it's turtles all the way down!” Overview: Reasoning about programs 2 ± Our broad problem: code is unlikely to be correct if we don’t have good reasons for believing it works ± We need clear problem statements ± And then a rigorous way to convince ourselves that what we wrote solves the problem ± But reasoning about programs can be hard ± Especially with recursion, concurrency ± Today focus on recursion Overview: Reasoning about programs 3 ± Recursion ± A programming strategy that solves a problem by reducing it to simpler or smaller instance(s) of the same problem ± Induction ± A mathematical strategy for proving statements about natural numbers 0,1,2,. .. (or more generally, about inductively defined objects ) ± They are very closely related ± Induction can be used to establish the correctness and complexity of programs Defining Functions 4 ± It is often useful to describe a function in different ways ± Let S : int int be the function where S(n) is the sum of the integers from 0 to n. For example, S(0) = 0 S(3) = 0+1+2+3 = 6 ± Definition: iterative form ² S(n) = 0+1+ … + n = Σ i ± Another characterization: closed form ² S(n) = n(n+1)/2 n i=0 Sum of Squares 5 ± A more complex example ± Let SQ : int int be the function that gives the sum of the squares of integers from 0 to n: SQ(0) = 0 SQ(3) = 0 2 + 1 2 + 2 2 + 3 2 = 14 ± Definition (iterative form): SQ(n) = 0 2 + 1 2 + … + n 2 ± Is there an equivalent closed-form expression? Closed-Form Expression for SQ(n) 6 ± Sum of integers between 0 through n was n(n+1)/2 which is a quadratic in n (that is, O(n 2 )) ± Inspired guess: perhaps sum of squares of integers between 0 through n is a cubic in n ± Conjecture: SQ(n) = an 3 +bn 2 +cn+d where a, b, c, d are unknown coefficients ± How can we find the values of the four unknowns? ± Idea: Use any 4 values of n to generate 4 linear equations, and then solve

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
11/12/2009 2 Finding Coefficients 7 ± Use n = 0, 1, 2, 3 ± SQ(0) = 0 = a·0 + b·0 + c·0 + d SQ(n) = 0 2 +1 2 +…+n 2 = an 3 +bn 2 +cn+d = ± SQ(1) = 1 = a·1 + b·1 + c·1 + d ± SQ(2) = 5 = a·8 + b·4 + c·2 + d ± SQ(3) = 14 = a·27 + b·9 + c·3 + d ± Solve these 4 equations to get ² a = 1/3 b = 1/2 c = 1/6 d = 0 Is the Formula Correct?
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 03/08/2010 for the course CS 2110 taught by Professor Francis during the Spring '07 term at Cornell University (Engineering School).

### Page1 / 5

L22cs2110fa09-6up - 11/12/2009 Overview: Reasoning about...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online