Package com.guinetik.examples
Class FibonacciSequence
java.lang.Object
com.guinetik.examples.FibonacciSequence
A utility class for generating and working with Fibonacci sequences.
Demonstrates various approaches to calculating Fibonacci numbers including iterative, recursive, and memoized implementations. This class is designed to showcase code coverage and documentation in Terminal Javadocs.
Fibonacci Definition
The Fibonacci sequence is defined as:
F(0) = 0
F(1) = 1
F(n) = F(n-1) + F(n-2) for n > 1
Usage Example
FibonacciSequence fib = new FibonacciSequence();
long number = fib.calculate(10); // Returns 55
List<Long> sequence = fib.generateSequence(5);
- Since:
- 2025
- Version:
- 1.0.0
- Author:
- guinetik
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe maximum index for iterative calculation to avoid overflow -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new FibonacciSequence calculator with an empty cache. -
Method Summary
Modifier and TypeMethodDescriptionlongcalculate(int n) Calculates the nth Fibonacci number using iterative approach.longcalculateMemoized(int n) Calculates the nth Fibonacci number using recursive approach with memoization.voidClears the memoization cache.longfindFirstGreaterThan(long threshold) Finds the first Fibonacci number that is greater than or equal to the threshold.generateSequence(int count) Generates a Fibonacci sequence up to the specified count.intGets the current size of the memoization cache.booleanisFibonacciNumber(long number) Checks if a number is in the Fibonacci sequence.static voidMain entry point demonstrating Fibonacci sequence calculations.longsumSequence(int n) Calculates the sum of the first n Fibonacci numbers.
-
Field Details
-
MAX_SAFE_INDEX
public static final int MAX_SAFE_INDEXThe maximum index for iterative calculation to avoid overflow- See Also:
-
-
Constructor Details
-
FibonacciSequence
public FibonacciSequence()Creates a new FibonacciSequence calculator with an empty cache.
-
-
Method Details
-
calculate
public long calculate(int n) Calculates the nth Fibonacci number using iterative approach.This is the most efficient approach for most use cases, with O(n) time complexity and O(1) space complexity.
- Parameters:
n- the index of the Fibonacci number to calculate (0-based)- Returns:
- the nth Fibonacci number
- Throws:
IllegalArgumentException- if n is negative or exceeds MAX_SAFE_INDEX
-
calculateMemoized
public long calculateMemoized(int n) Calculates the nth Fibonacci number using recursive approach with memoization.This approach uses caching to avoid redundant calculations. The first call will be slower, but subsequent calls benefit from the cache.
- Parameters:
n- the index of the Fibonacci number to calculate (0-based)- Returns:
- the nth Fibonacci number
- Throws:
IllegalArgumentException- if n is negative
-
generateSequence
Generates a Fibonacci sequence up to the specified count.- Parameters:
count- the number of Fibonacci numbers to generate- Returns:
- a list containing the first 'count' Fibonacci numbers
- Throws:
IllegalArgumentException- if count is negative
-
findFirstGreaterThan
public long findFirstGreaterThan(long threshold) Finds the first Fibonacci number that is greater than or equal to the threshold.- Parameters:
threshold- the minimum value to find- Returns:
- the first Fibonacci number >= threshold, or -1 if none found within safe range
- Throws:
IllegalArgumentException- if threshold is negative
-
sumSequence
public long sumSequence(int n) Calculates the sum of the first n Fibonacci numbers.- Parameters:
n- the count of Fibonacci numbers to sum- Returns:
- the sum of the first n Fibonacci numbers
- Throws:
IllegalArgumentException- if n is negative
-
clearCache
public void clearCache()Clears the memoization cache.This resets the cache to only contain the base cases (0 and 1).
-
getCacheSize
public int getCacheSize()Gets the current size of the memoization cache.- Returns:
- the number of cached values
-
isFibonacciNumber
public boolean isFibonacciNumber(long number) Checks if a number is in the Fibonacci sequence.- Parameters:
number- the number to check- Returns:
- true if the number is a Fibonacci number, false otherwise
-
main
Main entry point demonstrating Fibonacci sequence calculations.- Parameters:
args- command line arguments (first argument is optional index)
-