Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion code_to_optimize/java/src/main/java/com/example/Algorithms.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,30 @@ public long fibonacci(int n) {
if (n <= 1) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
// Fast-doubling iterative approach:
// Maintain (a, b) = (F(k), F(k+1)) and process bits of n from MSB to LSB.
long a = 0L; // F(0)
long b = 1L; // F(1)

int highestBit = 31 - Integer.numberOfLeadingZeros(n); // position of highest set bit
int mask = 1 << highestBit;
for (; mask != 0; mask >>>= 1) {
// Loop invariant: (a, b) = (F(k), F(k+1)) for current k
long twoBminusA = (b << 1) - a; // 2*b - a
long c = a * twoBminusA; // F(2k) = F(k) * (2*F(k+1) − F(k))
long d = a * a + b * b; // F(2k+1) = F(k)^2 + F(k+1)^2

if ((n & mask) == 0) {
// bit is 0 -> (F(2k), F(2k+1))
a = c;
b = d;
} else {
// bit is 1 -> (F(2k+1), F(2k+2)) = (d, c + d)
a = d;
b = c + d;
}
}
return a;
}

/**
Expand Down
Loading