2015-04-20 15:36:01 -07:00
|
|
|
|
//: generics/Fibonacci.java
|
2015-05-29 14:18:51 -07:00
|
|
|
|
// <20>2015 MindView LLC: see Copyright.txt
|
2015-04-20 15:36:01 -07:00
|
|
|
|
// Generate a Fibonacci sequence.
|
|
|
|
|
import net.mindview.util.*;
|
|
|
|
|
|
|
|
|
|
public class Fibonacci implements Generator<Integer> {
|
|
|
|
|
private int count = 0;
|
2015-05-05 11:20:13 -07:00
|
|
|
|
@Override
|
2015-04-20 15:36:01 -07:00
|
|
|
|
public Integer next() { return fib(count++); }
|
|
|
|
|
private int fib(int n) {
|
|
|
|
|
if(n < 2) return 1;
|
|
|
|
|
return fib(n-2) + fib(n-1);
|
|
|
|
|
}
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
Fibonacci gen = new Fibonacci();
|
|
|
|
|
for(int i = 0; i < 18; i++)
|
|
|
|
|
System.out.print(gen.next() + " ");
|
|
|
|
|
}
|
|
|
|
|
} /* Output:
|
|
|
|
|
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584
|
|
|
|
|
*///:~
|