package c4; public class FibTest { public static void main(String []args ){ long begin = System.currentTimeMillis() ; System.out.println(fib(10)) ; long end = System.currentTimeMillis() ; System.out.println(end-begin) ; } public static long fib(int n){ if (n<=2){ return 1; }else { long s1 =1; long s2=1; for(int i=2;i<n-1;i++){ s2 = s1+s2; s1 = s2-s1 ; System.out.println("s1:"+s1+" s2:"+s2); } return s1+s2; } } }
结果为
s1:1 s2:2 s1:2 s2:3 s1:3 s2:5 s1:5 s2:8 s1:8 s2:13 s1:13 s2:21 s1:21 s2:34 55 1