{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

6 Recursion - Recursion 2 Arises in two forms in computer...

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

View Full Document Right Arrow Icon
12/09/2010 1 RECURSION Lecture 6 CS2110 – Fall 2009 Recursion 2 Arises in two forms in computer science We’ll explore both Recursion as a mathematical tool for defining a Recursion as a tool for defining a function in terms of its own value in a simpler case Recursion as a programming tool. You’ve seen this previously but we’ll take it to mind-bending extremes (by the end of the class it will seem easy!) Recursion as a math technique 3 Broadly, recursion is a powerful technique for specifying functions, sets, and programs Example recursively-defined functions and programs factorial combinations exponentiation (raising to an integer power) Example recursively-defined sets grammars expressions data structures (lists, trees, ...) The Factorial Function (n!) 4 Define n! = n·(n 1)·(n 2)···3·2·1 read: “n factorial” E.g., 3! = 3·2·1 = 6 By convention, 0! = 1 The function int int that gives n! on input n is called the factorial function The Factorial Function (n!) 5 n! is the number of permutations of n distinct objects There is just one permutation of one object. 1! = 1 There are two permutations of two objects: 2! = 2 There are two permutations of two objects: 2! = 2 1 2 2 1 There are six permutations of three objects: 3! = 6 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 If n > 0, n! = n·(n 1)! Permutations of 6 Permutations of non-orange blocks Total number = 4·3! = 4·6 = 24: 4! Each permutation of the three non- orange blocks gives four permutations when the orange block is included
Background image of page 1

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

View Full Document Right Arrow Icon