Merge pull request #2022 from codeflash-ai/codeflash/optimize-Fibonacci.fibonacci-mnqq81k5
Some checks are pending
Lint / prek (pull_request) Waiting to run
Claude Code / pr-review (pull_request) Waiting to run
Claude Code / claude-mention (pull_request) Waiting to run
CodeFlash / Optimize new Python code (pull_request) Waiting to run
E2E - Async / async-optimization (pull_request) Waiting to run
E2E - Bubble Sort Benchmark / benchmark-bubble-sort-optimization (pull_request) Waiting to run
E2E - Bubble Sort Pytest (No Git) / bubble-sort-optimization-pytest-no-git (pull_request) Waiting to run
E2E - Bubble Sort Unittest / bubble-sort-optimization-unittest (pull_request) Waiting to run
Coverage E2E / end-to-end-test-coverage (pull_request) Waiting to run
E2E - Futurehouse Structure / futurehouse-structure (pull_request) Waiting to run
E2E - Init Optimization / init-optimization (pull_request) Waiting to run
E2E - Java Fibonacci (No Git) / java-fibonacci-optimization-no-git (pull_request) Waiting to run
E2E - Java Tracer / java-tracer-e2e (pull_request) Waiting to run
E2E - JS CommonJS Function / js-cjs-function-optimization (pull_request) Waiting to run
E2E - JS ESM Async / js-esm-async-optimization (pull_request) Waiting to run
E2E - JS TypeScript Class / js-ts-class-optimization (pull_request) Waiting to run
E2E - Topological Sort (Worktree) / topological-sort-worktree-optimization (pull_request) Waiting to run
E2E - Tracer Replay / tracer-replay (pull_request) Waiting to run
Java E2E Tests / java-e2e (pull_request) Waiting to run
PR Labeler / label-workflow-changes (pull_request) Waiting to run
Mypy Type Checking for CLI / type-check-cli (pull_request) Waiting to run
unit-tests / unit-tests (ubuntu-latest, 3.10) (pull_request) Waiting to run
unit-tests / unit-tests (ubuntu-latest, 3.11) (pull_request) Waiting to run
unit-tests / unit-tests (ubuntu-latest, 3.12) (pull_request) Waiting to run
unit-tests / unit-tests (ubuntu-latest, 3.13) (pull_request) Waiting to run
unit-tests / unit-tests (ubuntu-latest, 3.14) (pull_request) Waiting to run
unit-tests / unit-tests (ubuntu-latest, 3.9) (pull_request) Waiting to run
unit-tests / unit-tests (windows-latest, 3.13) (pull_request) Waiting to run

️ Speed up method `Fibonacci.fibonacci` by 2,036%
This commit is contained in:
claude[bot] 2026-04-09 08:22:52 +00:00 committed by GitHub
commit 5ce36eddce
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -9,7 +9,7 @@ import java.util.List;
public class Fibonacci {
/**
* Calculate the nth Fibonacci number using recursion.
* Calculate the nth Fibonacci number using an efficient iterative fast-doubling algorithm.
*
* @param n Position in Fibonacci sequence (0-indexed)
* @return The nth Fibonacci number
@ -21,7 +21,28 @@ public class Fibonacci {
if (n <= 1) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
long a = 0L; // F(0)
long b = 1L; // F(1)
// Iterate from the highest bit of n down to the lowest.
for (int mask = Integer.highestOneBit(n); mask != 0; mask >>= 1) {
// Apply doubling formulas:
// F(2k) = F(k) * (2*F(k+1) - F(k))
// F(2k+1) = F(k+1)^2 + F(k)^2
long twoBMinusA = (b << 1) - a;
long c = a * twoBMinusA; // F(2k)
long d = a * a + b * b; // F(2k+1)
if ((n & mask) == 0) {
a = c;
b = d;
} else {
a = d;
b = c + d;
}
}
return a;
}
/**