Skip to content

Instantly share code, notes, and snippets.

@nattaphonjeamjit
Created March 26, 2019 07:41
Show Gist options
  • Save nattaphonjeamjit/792939fbdd45a217eb07dcfa7013b6ce to your computer and use it in GitHub Desktop.
Save nattaphonjeamjit/792939fbdd45a217eb07dcfa7013b6ce 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) {
AtomicInteger i = new AtomicInteger(0);
NavigableMap<Integer, Integer> map = new TreeMap<>();
Stream.iterate(Fibonacci.BASE, Fibonacci::next).limit(number + 1).forEach(fibonacci -> {
map.put(i.getAndIncrement(), fibonacci.previous);
});
//System.out.println(map);
return map.lastEntry().getValue();
}
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