Beruflich Dokumente
Kultur Dokumente
Introduction
A function call itself is called a recursive function. Recursion is a repetitive process in which an algorithm or function calls itself repeatedly until some specified condition has been satisfied. The recursive function must include a stopping condition (anchor condition) or else the function would never terminate (indefinite loop). Example of the use of recursive algorithms: Factorial, Fibonacci sequence (number) and Tower of Hanoi.
Problem 1: Factorial
The product of the integral positive integers from 1 to n, is called n factorial. Denoted by n! Factorial (n) = 1 for n=0 Factorial (n) = n * (n 1) * (n 2) *. * 1 for n>0 = n * factorial (n-1) for n>0
Example of factorial
3! 4! 5! 6! = = = = 3 4 5 6 * * * * 2 3 4 5 * * * * 1 2*1 3*2*1 4*3*2*1 = = = = 6 24 120 720
4. Return fact
Programming
/*Factorial (n) computation using iteration*/ #include <stdio.h> #include <conio.h> int fact (int ) void main() { int fact (int); int number, factnum; printf(\nEnter a number: "); scanf( %d,&number); factnum=fact(number); printf("\nThe factorial for ); printf(%d! = %d",number,factnum); } /*Iterative factorial computation*/ int fact (int number) { int i, fact=1; for(i=number;i>1;i--) { fact=fact*i; } return fact; } Output: Enter a number: 4 The factorial of 4! = 24
Programming
//Factorial (n) computation using recursion #include <stdio.h> #include <conio.h> void main() { int fact (int); int num,factnum; printf(\nEnter a number: "); scanf( %d, &num); factnum=fact(num); printf("\nThe factorial of ); printf(" %d!=%d", num, factnum); } //Recursive factorial int fact (int num) { if(number==0) { return 1; } else { return (num*fact(num-1)); } }
Output: Enter a number: 4 The factorial of 4! = 24
factorial (4)
24
factorial (4) = 24 return 4*6
n=4 Since n!<1 return 4 * factorial(3) factorial (3) n=3 Since n!<1 return 3* factorial(2) factorial (2)
n=2 Since n!<1 return 2* factorial(1) factorial (1) n=1 Since n!<1 return 1* factorial(0) factorial (0) n=0 Since n<1 return 1
Problem 2: Fibonacci
The Fibonacci sequence number is defined as 0, 1, 1, 2, 3, 5, 8, 13, 21, . . . Each number after the first two is the sum of the two preceding number.
F0 = 0 F1 = 1 Fn = Fn-1 + Fn-2 for n>=2
Question
What is the next 2 terms of the following Fibonacci sequence number?
0, 1, 1, 2, 3, 5, 8, 13, 21, ?, ?
Example of fibonacci
F(0) F(1) F(2) F(3) F(4) F(5) F(6) = = = = = = = 0 1 0 1 1 2 3
+ + + + +
1 1 2 3 5
= = = = =
1 2 3 5 8
Fibonacci algorithm
1. If (num = 0 or num = 1) Stopping Condition (for which the function does not call/refer to itself) 1.1 Return num Return (fib(num-1) + fib(num-2)
2.
Programming
//Fibbonaci sequence using recursion #include <stdio.h> #include <conio.h> void main() { int fib(int); int num; printf("\nEnter num for fibonacci sequence: "); scanf("%d",&num); printf("\nFibonacci %d = %d",n,fib(num)); getch(); int fib(int n) { if (num==0||num==1) return num; } return (fib(num-1)+fib(num-2));
B Initial
Step 1: A -> C
Continue
Step 2: A -> B
Step 3: C -> B
Continue
Step 4: A -> C
Step 5: B -> A
Continue
Step 6: B -> C
Step 7: A -> C
Else
2.1 2.2 2.3 2.4 2.5 Begin Move(numDisks-1, from, intermediate, destination) Print (Move, numDisks, from, from to, destination) Move(numDisks-1,intermediate, destination , from) End
//Tower of Hanoi #include <stdio.h> #include <conio.h> void main() { void move(int, char,char,char); int numDisk; printf("\nEnter number of disks: "); scanf(" %d", &numDisk); if(numDisk>0) move(numDisk , 'A','C','B'); getch(); }Enter number of disks: 3
Programming
void move(int numDisk, char from, char destination , char intermediate) { if (numDisk==1) printf("\nMove disk 1 from needle %c to needle %c", from,to); else { move(numDisk-1, from, intermediate, destination); printf("\nMove disk %d from needle %c to needle %c",numDisk,from,to); move(numDisk-1, intermediate, destination , from); } }
Move disk 1 from needle A to needle C Move disk 2 from needle A to needle B Move disk 1 from needle C to needle B Move disk 3 from needle A to needle C Move disk 1 from needle B to needle A Move disk 2 from needle B to needle C Move disk 1 from needle A to needle C
A ->C
A ->B
Move(1,C,B,A)
C ->B
Move(3,A,C,B)
A ->C
Move(1,B,A,C) Move(2,B,C,A)
B ->A
B ->C
Move(1,A,C,B)
A ->C
Exercise
Lab sheet 1: Recursion