(7)recursion

(7)recursion - RECURSION RECURSION Self referential...

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

View Full Document Right Arrow Icon
RECURSION RECURSION Self referential functions are called recursive (i.e. functions calling themselves) Recursive functions are very useful for many mathematical operations
Background image of page 1

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

View Full DocumentRight Arrow Icon
Factorial: Recursive Functions Factorial: Recursive Functions 1! = 1 2! = 2*1 = 2*1! So: n! = n*(n-1)! For N>1 3! = 3*2*1 = 3*2! 1! = 1 4! = 4*3*2*1 = 4 * 3! Properties of recursive functions: 1) what is the first case (terminal condition) 2) how is the n th case related to the (n-1) th case Or more general: how is n th case related to <n th case
Background image of page 2
Recursive procedure Recursive procedure A recursive task is one that calls itself . With each invocation, the problem is reduced to a smaller task ( reducing case ) until the task arrives at a terminal or base case, which stops the process. The condition that must be true to achieve the terminal case is the terminal condition .
Background image of page 3

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

View Full DocumentRight Arrow Icon
Background image of page 4
#include <iostream.h> long factorial(int); // function prototype int main() { int n; long result; cout << "Enter a number: "; cin >> n; result = factorial(n); cout << "\nThe factorial of " << n << " is " << result << endl; return 0; } long factorial(int n) { if (n == 1) return n; else return n * factorial(n-1); } Terminal condition to end recursion
Background image of page 5

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

View Full DocumentRight Arrow Icon
long factorial(int n) { if (n == 1) return n; else /* L#6 */ return n * factorial(n-1); } Addr of calling statement Reserved for return value n=3 Main program: result = factorial(3) n=2 Return value Addr of calling statement L#6: return 3 * factorial(2) Addr of calling statement Return value n=1 L#6: return 2 * factorial(1) 1 2*1 3*2
Background image of page 6
int factorial (int n) { int fact; for (fact = 1;n>0; n--) fact=fact*n; return fact } Iterative solution for factorial: Recursive functions can always be “unwound” and written iteratively
Background image of page 7

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

View Full DocumentRight Arrow Icon
Example 2: Power function Example 2: Power function x n = 1 if n = 0 = x * x n-1 if n>0 5 3 = 5 * 5 2 5 2 = 5 * 5 1 5 1 = 5 * 5 0 5 0 = 1
Background image of page 8
float power(float x, int n) { if (n == 0) return 1; else return x * power (x, n-1); } 5 * power(5,2) Power(5,3) 5 * power(5,0) 5 * power(5,1) Return 1 X=5, n=3 X=5, n=2 X=5, n=1 X=5, n=0
Background image of page 9

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

View Full DocumentRight Arrow Icon
Power function x Power function x n n Thus for n= 3, the recursive function is called 4 times, with n=3, n=2, n=1, and n=0 in general for a power of n, the function is called n+1 times. Can we do better?
Background image of page 10
We know that : x 14 = (x 7 ) 2 in other words: (x 7 * x 7 ) x 7 = x (x 3 ) 2 int (7/2) = 3 x 3 = x (x 1 ) 2 x 1 = x (x 0 ) 2 x 0 = 1 x n = 1 if n= 0 = x(x n/2 ) 2 if n is odd = (x n/2 ) 2 if n is even Floor of n/2
Background image of page 11

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

View Full DocumentRight Arrow Icon
float power (float x, int n) { float HalfPower; //terminal condition if (n == 0) return 1; //can also stop at n=1 //if n Is odd if (n%2 == 1) { HalfPower = power(x,n/2); return (x * HalfPower *HalfPower): } else { HalfPower = power(x,n/2); return (HalfPower * HalfPower);
Background image of page 12
if (n == 0) return 1; if (n%2 == 1) { HalfPower = power(x,n/2); return (x * HalfPower *HalfPower): } else { HalfPower = power(x,n/2); return (HalfPower * HalfPower); } Can also use the call: return power(x,n/2) * power (x,n/2) But that is much less efficient!
Background image of page 13

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

View Full DocumentRight Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 40

(7)recursion - RECURSION RECURSION Self referential...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online