Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -1,4 +1,28 @@
|
|||
public static long recFibN(final int n)
|
||||
{
|
||||
return (n < 2) ? n : recFibN(n - 1) + recFibN(n - 2);
|
||||
/**
|
||||
* O(log(n))
|
||||
*/
|
||||
public static long fib(long n) {
|
||||
if (n <= 0)
|
||||
return 0;
|
||||
|
||||
long i = (int) (n - 1);
|
||||
long a = 1, b = 0, c = 0, d = 1, tmp1,tmp2;
|
||||
|
||||
while (i > 0) {
|
||||
if (i % 2 != 0) {
|
||||
tmp1 = d * b + c * a;
|
||||
tmp2 = d * (b + a) + c * b;
|
||||
a = tmp1;
|
||||
b = tmp2;
|
||||
}
|
||||
|
||||
tmp1 = (long) (Math.pow(c, 2) + Math.pow(d, 2));
|
||||
tmp2 = d * (2 * c + d);
|
||||
|
||||
c = tmp1;
|
||||
d = tmp2;
|
||||
|
||||
i = i / 2;
|
||||
}
|
||||
return a + b;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,4 @@
|
|||
public static long anFibN(final long n)
|
||||
public static long recFibN(final int 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));
|
||||
return (n < 2) ? n : recFibN(n - 1) + recFibN(n - 2);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,6 @@
|
|||
public static long fibTailRec(final int n)
|
||||
public static long anFibN(final long 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);
|
||||
double p = (1 + Math.sqrt(5)) / 2;
|
||||
double q = 1 / p;
|
||||
return (long) ((Math.pow(p, n) + Math.pow(q, n)) / Math.sqrt(5));
|
||||
}
|
||||
|
|
|
|||
9
Task/Fibonacci-sequence/Java/fibonacci-sequence-5.java
Normal file
9
Task/Fibonacci-sequence/Java/fibonacci-sequence-5.java
Normal 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);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue