1 / 73

Example – factorial

#include &lt;stdio.h&gt; int factorial( int n) { int fact = 1; while (n &gt;= 1) { fact *=n; n--; } return fact; }. int main( void ) { int num, fact; printf(&quot;enter a number<br>&quot;); scanf(&quot;%d&quot;,&amp;num); fact = factorial(num); printf(&quot;%d! = %d<br>&quot;, num, fact); return 0;

irisa
Download Presentation

Example – factorial

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. #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

  2. (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

  3. 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?

  4. 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

  5. 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…

  6. 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*…

  7. 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); }

  8. 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); }

  9. 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); }

  10. 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); }

  11. 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); }

  12. 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); }

  13. 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); }

  14. 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); }

  15. 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); }

  16. 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); }

  17. 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); }

  18. 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); }

  19. 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

  20. Example rec_pow.c

  21. 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); }

  22. 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); }

  23. 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); }

  24. 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); }

  25. 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); }

  26. 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); }

  27. 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); }

  28. 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); }

  29. 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); }

  30. 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); }

  31. 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); }

  32. 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); }

  33. 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); }

  34. 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); }

  35. 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); }

  36. 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); }

  37. 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); }

  38. 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); }

  39. 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); }

  40. 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); }

  41. 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); }

  42. 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); }

  43. 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); }

  44. 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); }

  45. 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); }

  46. 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); }

  47. 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); }

  48. 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); }

  49. 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); }

  50. 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); }

More Related