Doan Nhat Quang Recursion 8 38 Recursion Recursion Definition Recursion in

# Doan nhat quang recursion 8 38 recursion recursion

• 53

This preview shows page 11 - 22 out of 53 pages.

Doan Nhat Quang Recursion 8 / 38 Recursion Recursion Definition Recursion in mathematics or in computer science is the process of repeating objects in a self-similar way . Example 1 Assume that we have a definition of natural numbers as following: I 0 N I if n N then n + 1 N I there are no other objects in the set N . Due to this definition of natural numbers N , thus 0, 0 + 1 = 1 are natural. Same for, 0+ 1 + 1 = 2 is natural, etc. Doan Nhat Quang Recursion 8 / 38 Recursion Recursion A recursive defnition consists of two parts : I In the first part, called the anchor or the base case , the basic elements that are the building blocks of all other elements of the set are listed. I In the second part, rules are given that allow for the construction of new objects out of basic elements or objects that have already been constructed. These rules are applied again and again to generate new objects . Doan Nhat Quang Recursion 9 / 38 Recursion Recursion Example 2 In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. I 0! = 1 base case I ( n + 1)! = ( n + 1) n ! rules for the construction of new objects According to this definition, we generate the sequence of the numbers 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800,... are respectively the factorials of the numbers 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,... Doan Nhat Quang Recursion 10 / 38 Recursion Recursion Proving recursive solutions correct is related to mathematical induction: I Also closely related to proof by induction I Start by proving a base case I Then show that if it is true for case n, it must also be true for case n+1 Doan Nhat Quang Recursion 11 / 38 Recursion Recursion in Programming I Methods can call other methods I can a method call itself? Yes! This is called a recursive method (function) Recursive methods: I Each call solves an identical problem I the code is the same! I successive calls solve smaller/simpler instances I Every recursive algorithm has at least one base case I a base case (often 1 or 0) Doan Nhat Quang Recursion 12 / 38 Recursion Recursive Methods Definition A function is recursive if it calls or defines itself during the execution (direct way). Doan Nhat Quang Recursion 13 / 38 Recursion Recursive Methods Definition A function is recursive if it calls or defines itself during the execution (direct way). Compute the factorial of a given integer n 1 i n t f a c t o r i a l ( i n t n) { 2 i f (n == 1) 3 r e t u r n 1; 4 e l s e 5 r e t u r n n * f a c t o r i a l (n - 1); 6 } Doan Nhat Quang Recursion 13 / 38 Recursion Recursive Methods 1 i n t main () { 2 unsigned i n t r = f a c t o r i a l ( 5 ) ; 3 } Doan Nhat Quang Recursion 14 / 38 Recursion Recursive Methods 1 i n t main () { 2 unsigned i n t r = f a c t o r i a l ( 5 ) ; 3 } The obtained results are as following: Main factorial(5) Call 1 factorial(4) Call 2 factorial(3) Call 3 factorial(2) Call 4 factorial(1) Result 4 1 Result 3 2*1 Result 2 3*2*1 Result 1 4*3*2*1 Result 5*4*3*2*1 = 120 Doan Nhat Quang Recursion 14 / 38 Recursion Recursive Methods Definition A function is indirectly recursive if it calls its invoker and eventually results in the original call.  #### You've reached the end of your free preview.

Want to read all 53 pages?

• Spring '18
• DK Daniels
• Recursion, Doan Nhat Quang
• • •  