introduction to computer science robert sedgewick and kevin wayne recursive factorial demo pubic...
TRANSCRIPT
![Page 1: Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {](https://reader036.vdocuments.site/reader036/viewer/2022062620/551a9adf5503466b3a8b53f4/html5/thumbnails/1.jpg)
Introduction to Computer Science • Robert Sedgewick and Kevin Wayne • http://www.cs.Princeton.EDU/IntroCS
Recursive Factorial Demo
pubic class Factorial { public static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); }
public static void main(String[] args) { System.out.println(fact(3)); }}
![Page 2: Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {](https://reader036.vdocuments.site/reader036/viewer/2022062620/551a9adf5503466b3a8b53f4/html5/thumbnails/2.jpg)
static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); }
fact(3)n = 3
environment
![Page 3: Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {](https://reader036.vdocuments.site/reader036/viewer/2022062620/551a9adf5503466b3a8b53f4/html5/thumbnails/3.jpg)
static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); }
fact(3)n = 3
environment
![Page 4: Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {](https://reader036.vdocuments.site/reader036/viewer/2022062620/551a9adf5503466b3a8b53f4/html5/thumbnails/4.jpg)
static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); }
fact(3)n = 3
environment
![Page 5: Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {](https://reader036.vdocuments.site/reader036/viewer/2022062620/551a9adf5503466b3a8b53f4/html5/thumbnails/5.jpg)
static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); }
fact(3)n = 3
environment
static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); }
fact(2)n = 2
environment
![Page 6: Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {](https://reader036.vdocuments.site/reader036/viewer/2022062620/551a9adf5503466b3a8b53f4/html5/thumbnails/6.jpg)
static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); }
fact(3)n = 3
environment
static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); }
fact(2)n = 2
environment
![Page 7: Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {](https://reader036.vdocuments.site/reader036/viewer/2022062620/551a9adf5503466b3a8b53f4/html5/thumbnails/7.jpg)
static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); }
fact(3)n = 3
environment
static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); }
fact(2)n = 2
environment
![Page 8: Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {](https://reader036.vdocuments.site/reader036/viewer/2022062620/551a9adf5503466b3a8b53f4/html5/thumbnails/8.jpg)
static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); }
fact(3)n = 3
environment
static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); }
fact(2)n = 2
environment
static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); }
fact(1)n = 1
environment
![Page 9: Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {](https://reader036.vdocuments.site/reader036/viewer/2022062620/551a9adf5503466b3a8b53f4/html5/thumbnails/9.jpg)
static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); }
fact(3)n = 3
environment
static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); }
fact(2)n = 2
environment
static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); }
fact(1)n = 1
environment
![Page 10: Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {](https://reader036.vdocuments.site/reader036/viewer/2022062620/551a9adf5503466b3a8b53f4/html5/thumbnails/10.jpg)
static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); }
fact(3)n = 3
environment
static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); }
fact(2)n = 2
environment
static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); }
fact(1)n = 1
environment
![Page 11: Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {](https://reader036.vdocuments.site/reader036/viewer/2022062620/551a9adf5503466b3a8b53f4/html5/thumbnails/11.jpg)
static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); }
fact(3)n = 3
environment
static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); }
fact(2)n = 2
environment
static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); }
fact(1)n = 1
environment
static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); }
fact(0)n = 0
environment
![Page 12: Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {](https://reader036.vdocuments.site/reader036/viewer/2022062620/551a9adf5503466b3a8b53f4/html5/thumbnails/12.jpg)
static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); }
fact(3)n = 3
environment
static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); }
fact(2)n = 2
environment
static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); }
fact(1)n = 1
environment
static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); }
fact(0)n = 0
environment
![Page 13: Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {](https://reader036.vdocuments.site/reader036/viewer/2022062620/551a9adf5503466b3a8b53f4/html5/thumbnails/13.jpg)
static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); }
fact(3)n = 3
environment
static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); }
fact(2)n = 2
environment
static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); }
fact(1)n = 1
environment
static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); }
fact(0)n = 0
environment
1
![Page 14: Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {](https://reader036.vdocuments.site/reader036/viewer/2022062620/551a9adf5503466b3a8b53f4/html5/thumbnails/14.jpg)
static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); }
fact(3)n = 3
environment
static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); }
fact(2)n = 2
environment
static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); }
fact(1)n = 1
environment
11
![Page 15: Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {](https://reader036.vdocuments.site/reader036/viewer/2022062620/551a9adf5503466b3a8b53f4/html5/thumbnails/15.jpg)
static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); }
fact(3)n = 3
environment
static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); }
fact(2)n = 2
environment
static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); }
fact(1)n = 1
environment
11
1
![Page 16: Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {](https://reader036.vdocuments.site/reader036/viewer/2022062620/551a9adf5503466b3a8b53f4/html5/thumbnails/16.jpg)
static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); }
fact(3)n = 3
environment
static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); }
fact(2)n = 2
environment
12
![Page 17: Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {](https://reader036.vdocuments.site/reader036/viewer/2022062620/551a9adf5503466b3a8b53f4/html5/thumbnails/17.jpg)
static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); }
fact(3)n = 3
environment
static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); }
fact(2)n = 2
environment
12
2
![Page 18: Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {](https://reader036.vdocuments.site/reader036/viewer/2022062620/551a9adf5503466b3a8b53f4/html5/thumbnails/18.jpg)
static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); }
fact(3)n = 3
environment
23
![Page 19: Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {](https://reader036.vdocuments.site/reader036/viewer/2022062620/551a9adf5503466b3a8b53f4/html5/thumbnails/19.jpg)
static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); }
fact(3)n = 3
environment
23
% java Factorial6
public class Factorial { public static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); }
public static void main(String[] args) { System.out.println(fact(3)); }}
6