-
Notifications
You must be signed in to change notification settings - Fork 21.2k
Expand file tree
/
Copy pathFibonacciJavaStreams.java
More file actions
48 lines (40 loc) · 2.19 KB
/
Copy pathFibonacciJavaStreams.java
File metadata and controls
48 lines (40 loc) · 2.19 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
package com.thealgorithms.maths;
import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
/**
* Calculates Fibonacci numbers using a functional programming paradigm with Java Streams.
* <p>
* This specific implementation uses {@link java.util.stream.Stream#iterate} and reductions to generate terms.
* <p>
* For alternative approaches to compute or verify Fibonacci numbers, see:
* <ul>
* <li>{@link com.thealgorithms.maths.FibonacciLoop} - Standard Iterative (Loop) approach</li>
* <li>{@link com.thealgorithms.recursion.FibonacciSeries} - Naive Recursive approach</li>
* <li>{@link com.thealgorithms.dynamicprogramming.Fibonacci} - Dynamic Programming approaches (Memoization, Bottom-Up, Optimized)</li>
* <li>{@link com.thealgorithms.maths.FibonacciNumberGoldenRation} - Closed-form expression using Binet's formula</li>
* <li>{@link com.thealgorithms.maths.FibonacciNumberCheck} - Utility to check if a given number is a Fibonacci number</li>
* <li>{@link com.thealgorithms.matrix.matrixexponentiation.Fibonacci} - O(log n) Matrix Exponentiation approach</li>
* </ul>
* * @author caos321
* @date 14 October 2021 (Thursday)
*/
public final class FibonacciJavaStreams {
private FibonacciJavaStreams() {
}
public static Optional<BigDecimal> calculate(final BigDecimal index) {
if (index == null || index.compareTo(BigDecimal.ZERO) < 0) {
throw new IllegalArgumentException("Input index cannot be null or negative!");
}
if (index.compareTo(BigDecimal.ONE) < 0) {
return Optional.of(BigDecimal.ZERO);
}
if (index.compareTo(BigDecimal.TWO) < 0) {
return Optional.of(BigDecimal.ONE);
}
final List<BigDecimal> results = Stream.iterate(index, x -> x.compareTo(BigDecimal.ZERO) > 0, x -> x.subtract(BigDecimal.ONE))
.reduce(List.of(), (list, current) -> list.isEmpty() || list.size() < 2 ? List.of(BigDecimal.ZERO, BigDecimal.ONE) : List.of(list.get(1), list.get(0).add(list.get(1))), (list1, list2) -> list1);
return results.isEmpty() ? Optional.empty() : Optional.of(results.get(results.size() - 1));
}
}