現在可以輕鬆寫了XD
import java.util.stream.Stream;
public class Test {
public static void main(String []args){
System.out.println(Fibonacci(10));
System.out.println(Old_Fibonacci(10));
}
private static long Fibonacci(int n) {
return Stream.iterate(new long[]{1, 1}, f -> new long[]{f[1], f[0] + f[1]})
.limit(n)
.reduce((in, out) -> out)
.get()[0];
}
private static long Old_Fibonacci(int n) {
if (n < 2)
return n;
else
return Old_Fibonacci(n - 2) + Old_Fibonacci(n - 1);
}
}