-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactorial.java
More file actions
107 lines (81 loc) · 4.39 KB
/
Copy pathFactorial.java
File metadata and controls
107 lines (81 loc) · 4.39 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package practice;
import java.math.BigInteger;
import java.util.stream.IntStream;
public class Factorial {
public static void main(String[] args) {
int number = 5;
long fact = 1;
for (int i = 1; i <= number; i++) {
fact *= i;
}
System.out.println("Factorial of " + number + " is: " + fact);
int n = 12;
int factorial = IntStream.rangeClosed(1, n).reduce(1, (a, b) -> a * b);
System.out.println("Factorial of " + number + " using Java 8 Streams is: " + factorial);
// Using Recursion
System.out.println("Factorial of " + number + " using recursion is: " + factorial(number));
// Using Java 8 Streams
System.out.println("Factorial of " + number + " using Java 8 Streams is: " + factorialUsingStreams(number));
// Using Java 8 Parallel Streams
System.out.println("Factorial of " + number + " using Java 8 Parallel Streams is: " + factorialUsingParallelStreams(number));
// Using Java 8 Streams with custom parallelism
System.out.println("Factorial of " + number + " using Java 8 Streams with custom parallelism is: " + factorialUsingParallelStreams(number, 10));
// // Using Java 8 Streams with custom parallelism and custom reduction
// System.out.println("Factorial of " + number + " using Java 8 Streams with custom parallelism and custom reduction is: " + factorialUsingParallelStreamsAndCustomReduction(number, 10));
// // Using Java 8 Streams with custom parallelism and custom reduction
// System.out.println("Factorial of " + number + " using Java 8 Streams with custom parallelism and custom reduction and parallelism level 10 is: " + factorialUsingParallelStreamsAndCustomReduction(number, 10, 10));
}
// Recursive method
public static long factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}
// Java 8 Streams method
public static long factorialUsingStreams(int n) {
return IntStream.rangeClosed(1, n)
.mapToObj(BigInteger::valueOf)
.reduce(BigInteger.ONE, BigInteger::multiply)
.longValue();
}
// Java 8 Parallel Streams method
public static long factorialUsingParallelStreams(int n) {
return IntStream.rangeClosed(1, n)
.parallel()
.mapToObj(BigInteger::valueOf)
.reduce(BigInteger.ONE, BigInteger::multiply)
.longValue();
}
// Java 8 Streams with custom parallelism
public static long factorialUsingParallelStreams(int n, int parallelism) {
return IntStream.rangeClosed(1, n)
.parallel()
.mapToObj(BigInteger::valueOf)
.reduce(BigInteger.ONE, BigInteger::multiply)
.longValue();
}
// Java 8 Streams with custom parallelism and custom reduction
// public static long factorialUsingParallelStreamsAndCustomReduction(int n, int parallelism, BiFunction<BigInteger, BigInteger, BigInteger> reductionFunction) {
// return IntStream.rangeClosed(1, n)
// .parallel()
// .mapToObj(BigInteger::valueOf)
// .reduce(BigInteger.ONE, reductionFunction);
// }
// Java 8 Streams with custom parallelism and custom reduction
// public static long factorialUsingParallelStreamsAndCustomReduction(int n, int parallelism) {
// return factorialUsingParallelStreamsAndCustomReduction(n, parallelism, BigInteger::multiply);
// }
// Java 8 Streams with custom parallelism and custom reduction and parallelism level 10
// public static long factorialUsingParallelStreamsAndCustomReduction(int n, int parallelism, int parallelismLevel) {
// return factorialUsingParallelStreamsAndCustomReduction(n, parallelism, parallelismLevel, BigInteger::multiply);
// }
// Java 8 Streams with custom parallelism and custom reduction and parallelism level 10
// public static long factorialUsingParallelStreamsAndCustomReduction(int n, int parallelism, int parallelismLevel, BiFunction<BigInteger, BigInteger, BigInteger> reductionFunction) {
// return IntStream.rangeClosed(1, n)
// .parallel()
// .mapToObj(BigInteger::valueOf)
// .reduce(BigInteger.ONE, reductionFunction);
// }
}