OnJava8-Examples/generics/Fibonacci.java
2015-12-15 11:47:04 -08:00

25 lines
727 B
Java

// generics/Fibonacci.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
// Generate a Fibonacci sequence.
import java.util.function.*;
public class Fibonacci implements Supplier<Integer> {
private int count = 0;
@Override
public Integer get() { 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.get() + " ");
}
}
/* Output:
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584
*/