HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Let's "Iterator" through Fibonacci, forever and ever

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
iteratorfibonacciforeverletthroughandever

Problem

Following-up to my previous question, this class is now based on an iterator. The class, instead of forcing the programmer to use up a bunch of memory just to get each progressive number once, they can now use an iterator. The class itself is also now based on an iterator, which makes my life easier too. No more initFirst()!

Edit note: the main method was there to test, and is there for no other reason.

```
import java.math.BigInteger;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
*
*/
public class InfiniteFibonacci {

public static void main(String[] args) {
InfiniteFibonacci i = new InfiniteFibonacci();
System.out.println(i.getNumberAt(0));
System.out.println(i.getNumberAt(100));
}

/**
* The default starting amount of numbers in the sequence to be generated.
*/
public static final int DEFAULT_STARTING_CAP = 64;

private final Iterator iterator = new FibonacciIterator();

private List parts;

private int currentSize = 0;

/**
* Creates a new InfiniteFibonacci object, with the default
* amount of numbers.
*/
public InfiniteFibonacci() {
this(DEFAULT_STARTING_CAP);
}

/**
* Creates a new InfiniteFibonacci object, with the specified
* amount of numbers.
*
* @param startCap
* The amount of numbers to generate. It will be rounded up to
* the nearest 16.
*/
public InfiniteFibonacci(int startCap) {
parts = new LinkedList<>();
initTo(startCap);
}

private void initTo(int index) {
for (; currentSize nth number in the sequence, zero-based.
*
* @param index
* the n as described
*
* @return the specified in the sequence, zero-based.
*/
public BigInteger getNumberAt(int index) {
if (index > currentSize) {
initTo(index);
}
return parts.get(index / SequencePar

Solution

Judging by the title of the question and the main class, I expected to see a simple iterator over Fibonacci numbers to infinity. Instead I see a class with too many responsibilities. It would be better to separate these concerns:

  • the infinite iterator



  • one class to get the nth Fibonacci number with caching



  • a utility class that implements the cache storage from a linked list of segments, the logic of which is encapsulated and hidden from users



  • the runner class with the main method



The Iterator is a very good idea. I thought of perhaps providing an Iterator too, but then, Fibonacci numbers tend to get very big very fast, so there's probably not much point. So just the Iterator makes sense.

What you did with the sequence parts is an interesting way to implement a dynamically growing array-like storage without resize operations. As mentioned, this has nothing to do with Fibonacci numbers, and this logic could live in its own utility class. But I think it's probably not worth it. You can use a simple ArrayList instead. When the performance of the ArrayList becomes a real concern, you could try your alternative List implementation.

Context

StackExchange Code Review Q#114708, answer score: 4

Revisions (0)

No revisions yet.