-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathRecursion.java
More file actions
41 lines (35 loc) Β· 810 Bytes
/
Copy pathRecursion.java
File metadata and controls
41 lines (35 loc) Β· 810 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package day4;
public class Recursion {
// inception
public static void main(String[] args) {
System.out.println(factorial(3));
}
// n! = n * (n - 1)!
// 5! = 5 * 4 * 3 * 2 * 1
// = 5 * 4!
// 4! = 4 * 3 * 2 * 1
//
// recursive step
// n! = n * (n - 1)!
// base case
// 0! = 1
/*
time complexity: O(number)
space complexity: O(number)
*/
private static long factorial(int number) {
if (number == 0) {
return 1;
}
return number * factorial(number - 1);
}
// 6
// func number = 3
// return 3 * 2
// factorial(2)
// return 2 * 1
// factorial(1)
// return 1 * 1
// factorial(0)
// return 1
}