Fibonacci sequence: Difference between revisions

→‎{{header|Java}}: Added analytic method
(→‎{{header|Logo}}: +Mathematica)
(→‎{{header|Java}}: Added analytic method)
Line 327:
if(n <= 2) return 1;
return recFibN(n-1) + recFibN(n-2);
}</java>
===Analytic===
This method works up to the 92<sup>nd</sup> Fibonacci number. After that, it goes out of range.
<java>public static long anFibN(long n){
double p= (1 + Math.sqrt(5)) / 2;
double q= 1 / p;
return (long)((Math.pow(p, n) + Math.pow(q, n)) / Math.sqrt(5));
}</java>
 
Anonymous user