-
Notifications
You must be signed in to change notification settings - Fork 2
/
fibo.pde
40 lines (39 loc) · 1.02 KB
/
fibo.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
* Fast Fibonacci algorithms (Java)
*
* Copyright (c) 2017 Project Nayuki
* All rights reserved. Contact Nayuki for licensing.
* https://www.nayuki.io/page/fast-fibonacci-algorithms
* adapted by cameyo for ForthCalc.pde (processing)
*/
private static BigInteger fastFibonacciDoubling(int n)
{
BigInteger a = BigInteger.ZERO;
BigInteger b = BigInteger.ONE;
int m = 0;
for (int bit = Integer.highestOneBit(n); bit != 0; bit >>>= 1)
{
// Loop invariant: a = F(m), b = F(m+1)
// Double it
BigInteger d = multiply(a, b.shiftLeft(1).subtract(a));
BigInteger e = multiply(a, a).add(multiply(b, b));
a = d;
b = e;
m *= 2;
// Advance by one conditionally
if ((n & bit) != 0)
{
BigInteger c = a.add(b);
a = b;
b = c;
m++;
}
}
return a;
}
// Multiplies two BigIntegers.
// This function makes it easy to swap in a faster algorithm like Karatsuba multiplication.
private static BigInteger multiply(BigInteger x, BigInteger y)
{
return x.multiply(y);
}