Skip to content

Instantly share code, notes, and snippets.

@pramoth
Forked from nattaphonjeamjit/fibo.java
Last active April 1, 2019 11:53
Show Gist options
  • Save pramoth/734271a72099b60a3bbfc9774ac6c8a5 to your computer and use it in GitHub Desktop.
Save pramoth/734271a72099b60a3bbfc9774ac6c8a5 to your computer and use it in GitHub Desktop.
import java.util.NavigableMap;
import java.util.TreeMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;
public class Fibonacci {
public static final Fibonacci BASE = new Fibonacci(0, 1);
public final int previous;
public final int current;
public Fibonacci(int previous, int current) {
this.previous = previous;
this.current = current;
}
public Fibonacci next() {
return new Fibonacci(current, current + previous);
}
public static int of(int number) {
return Stream.iterate(Fibonacci.BASE, Fibonacci::next)
.limit(number + 1)
.reduce((a,b)->b)
.map(e->e.previous).orElse(0);
}
public static void main(String[] args) {
System.out.println(Fibonacci.of(10));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment