Created
March 26, 2019 07:41
-
-
Save nattaphonjeamjit/792939fbdd45a217eb07dcfa7013b6ce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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