-
-
Save pramoth/734271a72099b60a3bbfc9774ac6c8a5 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) { | |
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