-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursive.java
More file actions
58 lines (53 loc) · 1.56 KB
/
Recursive.java
File metadata and controls
58 lines (53 loc) · 1.56 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package SortAlgorithms;
import java.time.Duration;
import java.time.Instant;
/**
* @author : J. Andrés Boyacá Silva
* @since : 8/27/2020, Thu
**/
public class Recursive {
public static void main(String[] args) {
Instant iterative = Instant.now();
System.out.println("iterativeFactorial " + iterativeFactorial(10));
Instant recursive = Instant.now();
System.out.println(Duration.between(iterative, recursive).toMillis());
System.out.println("recursiveFactorial " + recursiveFactorial(10));
Instant finished = Instant.now();
System.out.println(Duration.between(recursive, finished).toMillis());
}
/**
* 1! = 1 * 0! = 1
* 2! = 2 * 1 = 2 * 1!
* 3! = 3 * 2 * 1 = 3 * 2!
* @param num factorial wished
* @return factorial result
*/
public static int iterativeFactorial(int num) {
//breaking condition
if (num == 0) {
return 1;
}
int factorial = 1;
for (int i = 1; i <= num; i++) {
factorial *= i;
}
return factorial;
}
/**
* This method will have a 'calls stackables' therefore a lower performance
* and a probable stack over flow because of memory
* <p>
* !n = n * (n - 1)!
*
* @param num factorial wished
* @return factorial result
* @throws StackOverflowError
*/
public static int recursiveFactorial(int num) {
//breaking condition
if (num == 0) {
return 1;
}
return num * recursiveFactorial(num - 1);
}
}