This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,15 @@
public static long itFibN(int n)
{
if (n < 2)
return n;
long ans = 0;
long n1 = 0;
long n2 = 1;
for(n--; n > 0; n--)
{
ans = n1 + n2;
n1 = n2;
n2 = ans;
}
return ans;
}

View file

@ -0,0 +1,4 @@
public static long recFibN(final int n)
{
return (n < 2) ? n : recFibN(n - 1) + recFibN(n - 2);
}

View file

@ -0,0 +1,6 @@
public static long anFibN(final 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));
}

View file

@ -0,0 +1,9 @@
public static long fibTailRec(final int n)
{
return fibInner(0, 1, n);
}
private static long fibInner(final long a, final long b, final int n)
{
return n < 1 ? a : n == 1 ? b : fibInner(b, a + b, n - 1);
}