12/09/20101RECURSIONLecture 6CS2110 – Fall 2009Recursion2Arises in two forms in computer scienceWe’ll explore bothRecursion as amathematicaltool for defining aRecursion as a tool for defining a function in terms of its own value in a simpler caseRecursion as a programmingtool. 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 technique3Broadly, recursion is a powerful technique for specifying functions, sets, and programsExample recursively-defined functions and programsfactorial combinationsexponentiation (raising to an integer power)Example recursively-defined setsgrammars expressionsdata structures (lists, trees, ...)The Factorial Function (n!)4Define n! = n·(n−1)·(n−2)···3·2·1 read: “n factorial”E.g., 3! = 3·2·1 = 6By convention, 0! = 1The function int→int that gives n! on input n is called the factorial functionThe Factorial Function (n!)5n! is the number of permutations of n distinct objectsThere is just one permutation of one object. 1! = 1There are two permutations of two objects: 2! = 2There are two permutations of two objects: 2! = 21 2 2 1There are six permutations of three objects: 3! = 61 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1If n > 0, n! = n·(n −1)!Permutations of6Permutations of non-orange blocksTotal number = 4·3! = 4·6 = 24: 4!Each permutation of the three non-orange blocks gives four permutations when the orange block is included
has intentionally blurred sections.
Sign up to view the full version.