-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxFactorialFinder.java
More file actions
87 lines (70 loc) · 3.13 KB
/
Copy pathMaxFactorialFinder.java
File metadata and controls
87 lines (70 loc) · 3.13 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package practice;
import java.math.BigInteger;
import java.util.stream.IntStream;
public class MaxFactorialFinder {
public static void main(String[] args) {
int n = 1;
while (true) {
try {
long startTime = System.nanoTime();
// BigInteger factorial = calculateBigFactorial(n);
long endTime = System.nanoTime();
// Print Execution Time and Memory Usage
long executionTime = (endTime - startTime) / 1_000_000; // Convert to ms
long freeMemory = Runtime.getRuntime().freeMemory(); // Available memory in bytes
System.out.println(n + "! computed in " + executionTime + " ms | Free Memory: " + (freeMemory / (1024 * 1024)) + " MB");
//n++; // Increment and continue testing
} catch (OutOfMemoryError e) {
System.out.println("Max factorial reached before memory overflow: " + (n - 1) + "!");
break;
}
}
}
public static BigInteger calculateBigFactorial(int n) {
return IntStream.rangeClosed(1, n)
.mapToObj(BigInteger::valueOf)
.reduce(BigInteger.ONE, BigInteger::multiply);
}
public static BigInteger calculateFactorial(int n) {
return (n == 0) ? BigInteger.ONE :
IntStream.rangeClosed(1, n)
.mapToObj(BigInteger::valueOf)
.reduce(BigInteger.ONE, BigInteger::multiply);
}
}
/* import java.math.BigInteger;
import java.util.stream.IntStream;
public class MaxFactorialFinder {
public static void main(String[] args) {
int n = 1;
while (true) {
try {
long startTime = System.nanoTime();
// Compute factorial
BigInteger factorial = calculateBigFactorial(n);
long endTime = System.nanoTime();
long executionTime = (endTime - startTime) / 1_000_000; // Convert to ms
long freeMemory = Runtime.getRuntime().freeMemory() / (1024 * 1024); // Convert to MB
// Print factorial for small values, track time for large values
if (n <= 20) {
System.out.println(n + "! = " + factorial);
}
System.out.println(n + "! computed in " + executionTime + " ms | Free Memory: " + freeMemory + " MB");
// Stop when memory gets too low
if (freeMemory < 50) {
System.out.println("Stopping due to low memory! Last computed factorial: " + n + "!");
break;
}
n++; // Increment n for the next iteration
} catch (OutOfMemoryError e) {
System.out.println("Max factorial reached before memory overflow: " + (n - 1) + "!");
break;
}
}
}
public static BigInteger calculateBigFactorial(int n) {
return IntStream.rangeClosed(1, n)
.mapToObj(BigInteger::valueOf)
.reduce(BigInteger.ONE, BigInteger::multiply);
}
} */