740 likes | 925 Views
#include <stdio.h> int factorial( int n) { int fact = 1; while (n >= 1) { fact *=n; n--; } return fact; }. int main( void ) { int num, fact; printf("enter a number<br>"); scanf("%d",&num); fact = factorial(num); printf("%d! = %d<br>", num, fact); return 0;
E N D
#include <stdio.h> int factorial(int n) { int fact = 1; while (n >= 1) { fact *=n; n--; } return fact; } int main(void) { int num, fact; printf("enter a number\n"); scanf("%d",&num); fact = factorial(num); printf("%d! = %d\n", num, fact); return 0; } Example – factorial
(n-1)! Another way of looking at factorial • As we saw, n! = 1*2*3*… *(n-1)*n • Thus, we can also define factorial the following way: • 0! = 1 • n! = n*(n-1)! for n>0 *n
A recursive definition • C functions can also call themselves! • However, usually not with the same parameters (why?) • Some functions can be defined using smaller occurrences of themselves. • Such a definition is called a “recursive definition” of a function. • Every recursive function has a “boundary condition”. The function stops calling itself when it is satisfied. • Why is this necessary?
int factRec(int n) { if (n==0 || n==1) return 1; return n*factRec(n-1); } int factorial(int n) { int fact =1; while (n >= 1) { fact *=n; n--; } return fact; } Example - factorial
Recursive factorial – step by step FactRec(4) int factRec(int n) { if (n==0 || n==1) return 1; return n*factRec(n-1); } n 4 Returns…
Recursive factorial – step by step FactRec(4) int factRec(int n) { if (n==0 || n==1) return 1; return n*factRec(n-1); } n 4 Returns… 4*…
FactRec(4) FactRec(3) n n 4 3 Returns… Returns… Recursive factorial – step by step int factRec(int n) { if (n==0 || n==1) return 1; return n*factRec(n-1); }
FactRec(4) FactRec(3) n n 4 3 Returns… Returns… Recursive factorial – step by step int factRec(int n) { if (n==0 || n==1) return 1; return n*factRec(n-1); }
FactRec(4) FactRec(3) n n 4 3 Returns… Returns… 3*… Recursive factorial – step by step int factRec(int n) { if (n==0 || n==1) return 1; return n*factRec(n-1); }
FactRec(4) FactRec(3) FactRec(2) n n n 4 3 2 Returns… Returns… Returns… Recursive factorial – step by step int factRec(int n) { if (n==0 || n==1) return 1; return n*factRec(n-1); }
FactRec(4) FactRec(3) FactRec(2) n n n 4 3 2 Returns… Returns… Returns… Recursive factorial – step by step int factRec(int n) { if (n==0 || n==1) return 1; return n*factRec(n-1); }
FactRec(4) FactRec(3) FactRec(2) n n n 4 3 2 Returns… Returns… Returns… 2*… Recursive factorial – step by step int factRec(int n) { if (n==0 || n==1) return 1; return n*factRec(n-1); }
FactRec(4) FactRec(1) FactRec(3) FactRec(2) n n n n 2 1 3 4 Returns… Returns… Returns… Returns… Recursive factorial – step by step int factRec(int n) { if (n==0 || n==1) return 1; return n*factRec(n-1); }
FactRec(4) FactRec(1) FactRec(3) FactRec(2) n n n n 2 1 3 4 Returns… Returns… Returns… Returns… Recursive factorial – step by step int factRec(int n) { if (n==0 || n==1) return 1; return n*factRec(n-1); }
FactRec(4) FactRec(3) FactRec(2) FactRec(1) n n n n 2 3 4 1 Returns… Returns… Returns… Returns… 1 Recursive factorial – step by step int factRec(int n) { if (n==0 || n==1) return 1; return n*factRec(n-1); }
FactRec(4) FactRec(3) FactRec(2) n n n 4 3 2 Returns… Returns… Returns… 2*1 Recursive factorial – step by step int factRec(int n) { if (n==0 || n==1) return 1; return n*factRec(n-1); }
FactRec(4) FactRec(3) n n 4 3 Returns… Returns… 3*2 Recursive factorial – step by step int factRec(int n) { if (n==0 || n==1) return 1; return n*factRec(n-1); }
FactRec(4) n 4 Returns… 4*6 Recursive factorial – step by step int factRec(int n) { if (n==0 || n==1) return 1; return n*factRec(n-1); }
Another example - power • Xy = x*x*…*x • Recursive definitions (Assume non-negative y): • Base: x0=1 • Xy = x*(Xy-1) • Xy =(Xy/2)2 (for even y’s only) y times
Example rec_pow.c
rec_pow(2, 7) x y 2 7 Returns… rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }
rec_pow(2, 7) x y 2 7 Returns… rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }
rec_pow(2, 7) x y 2 7 Returns… rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }
rec_pow(2, 7) x y 2 7 Returns… rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if(y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }
rec_pow(2, 7) x y 2 7 Returns… 2*… rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if(y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }
rec_pow(2, 7) rec_pow(2, 6) x x y y 2 2 7 6 Returns… Returns… rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if(y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }
rec_pow(2, 7) rec_pow(2, 6) x x y y 2 2 7 6 Returns… Returns… rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if(y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }
rec_pow(2, 7) rec_pow(2, 6) x x y y 2 2 7 6 Returns… Returns… rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }
rec_pow(2, 7) rec_pow(2, 6) x x y y 2 2 7 6 Returns… Returns… square(…) rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if(y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }
rec_pow(2, 7) rec_pow(2, 3) rec_pow(2, 6) x x x y y y 2 2 2 6 3 7 Returns… Returns… Returns… square(…) rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if(y%2 == 0) returnsquare(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }
rec_pow(2, 7) rec_pow(2, 3) rec_pow(2, 6) x x x y y y 2 2 2 6 3 7 Returns… Returns… Returns… square(…) rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if(y%2 == 0) returnsquare(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }
rec_pow(2, 7) rec_pow(2, 3) rec_pow(2, 6) x x x y y y 2 2 2 6 3 7 Returns… Returns… Returns… square(…) rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) returnsquare(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }
rec_pow(2, 7) rec_pow(2, 3) rec_pow(2, 6) x x x y y y 2 2 2 6 3 7 Returns… Returns… Returns… square(…) rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if(y%2 == 0) returnsquare(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }
rec_pow(2, 7) rec_pow(2, 3) rec_pow(2, 6) x x x y y y 2 2 2 6 3 7 Returns… Returns… Returns… 2*… square(…) rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if(y%2 == 0) returnsquare(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }
rec_pow(2, 6) rec_pow(2, 7) rec_pow(2, 3) rec_pow(2, 2) x x x x y y y y 2 2 2 2 3 2 7 6 Returns… Returns… Returns… Returns… square(…) rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if(y%2 == 0) returnsquare(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }
rec_pow(2, 6) rec_pow(2, 7) rec_pow(2, 3) rec_pow(2, 2) x x x x y y y y 2 2 2 2 3 2 7 6 Returns… Returns… Returns… Returns… square(…) rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if(y%2 == 0) returnsquare(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }
rec_pow(2, 6) rec_pow(2, 7) rec_pow(2, 3) rec_pow(2, 2) x x x x y y y y 2 2 2 2 3 2 7 6 Returns… Returns… Returns… Returns… square(…) rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) returnsquare(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }
rec_pow(2, 6) rec_pow(2, 7) rec_pow(2, 3) rec_pow(2, 2) x x x x y y y y 2 2 2 2 3 2 7 6 Returns… Returns… Returns… Returns… square(…) square(…) rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if(y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }
rec_pow(2, 2) rec_pow(2, 7) rec_pow(2, 3) rec_pow(2, 6) rec_pow(2, 1) x x x x x y y y y y 2 2 2 2 2 2 1 6 7 3 Returns… Returns… Returns… Returns… Returns… square(…) rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if(y%2 == 0) returnsquare(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }
rec_pow(2, 2) rec_pow(2, 7) rec_pow(2, 3) rec_pow(2, 6) rec_pow(2, 1) x x x x x y y y y y 2 2 2 2 2 2 1 6 7 3 Returns… Returns… Returns… Returns… Returns… square(…) rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if(y%2 == 0) returnsquare(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }
rec_pow(2, 2) rec_pow(2, 7) rec_pow(2, 3) rec_pow(2, 6) rec_pow(2, 1) x x x x x y y y y y 2 2 2 2 2 2 1 6 7 3 Returns… Returns… Returns… Returns… Returns… square(…) rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if(y%2 == 0) returnsquare(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }
rec_pow(2, 2) rec_pow(2, 7) rec_pow(2, 3) rec_pow(2, 6) rec_pow(2, 1) x x x x x y y y y y 2 2 2 2 2 2 1 6 7 3 Returns… Returns… Returns… Returns… Returns… square(…) rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) returnsquare(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }
rec_pow(2, 2) rec_pow(2, 7) rec_pow(2, 3) rec_pow(2, 6) rec_pow(2, 1) x x x x x y y y y y 2 2 2 2 2 2 1 6 7 3 Returns… Returns… Returns… Returns… Returns… square(…) rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if(y%2 == 0) returnsquare(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }
rec_pow(2, 2) rec_pow(2, 7) rec_pow(2, 3) rec_pow(2, 6) rec_pow(2, 1) x x x x x y y y y y 2 2 2 2 2 2 1 6 7 3 Returns… Returns… Returns… Returns… Returns… square(…) 2*… rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if(y%2 == 0) returnsquare(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }
rec_pow(2, 7) rec_pow(2, 2) rec_pow(2, 1) rec_pow(2, 0) rec_pow(2, 6) rec_pow(2, 3) x x x x x x y y y y y y 2 2 2 2 2 2 2 1 0 3 6 7 Returns… Returns… Returns… Returns… Returns… Returns… square(…) rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if(y%2 == 0) returnsquare(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }
rec_pow(2, 7) rec_pow(2, 2) rec_pow(2, 1) rec_pow(2, 0) rec_pow(2, 6) rec_pow(2, 3) x x x x x x y y y y y y 2 2 2 2 2 2 2 1 0 3 6 7 Returns… Returns… Returns… Returns… Returns… Returns… square(…) rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if(y%2 == 0) returnsquare(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }
rec_pow(2, 7) rec_pow(2, 2) rec_pow(2, 1) rec_pow(2, 0) rec_pow(2, 6) rec_pow(2, 3) x x x x x x y y y y y y 2 2 2 2 2 2 2 1 0 3 6 7 Returns… Returns… Returns… Returns… Returns… Returns… square(…) 1 rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if(y%2 == 0) returnsquare(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }
rec_pow(2, 2) rec_pow(2, 7) rec_pow(2, 3) rec_pow(2, 6) rec_pow(2, 1) x x x x x y y y y y 2 2 2 2 2 2 1 6 7 3 Returns… Returns… Returns… Returns… Returns… square(…) 2*1 rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if(y%2 == 0) returnsquare(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }
rec_pow(2, 6) rec_pow(2, 7) rec_pow(2, 3) rec_pow(2, 2) x x x x y y y y 2 2 2 2 3 2 7 6 Returns… Returns… Returns… Returns… square(…) square(2) rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if(y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }
rec_pow(2, 7) rec_pow(2, 3) rec_pow(2, 6) x x x y y y 2 2 2 6 3 7 Returns… Returns… Returns… 2*4 square(…) rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if(y%2 == 0) returnsquare(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }