From b6f5f81bf0ce9f2db136ba638e4d2352176ad201 Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Fri, 6 Feb 2026 20:33:16 +0000 Subject: [PATCH] Optimize Algorithms.fibonacci MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The optimized code achieves a **30% runtime improvement** by replacing the exponential-time recursive Fibonacci algorithm with an iterative fast-doubling algorithm that runs in **O(log n) time complexity** instead of **O(2^n)**. **Key Changes:** 1. **Algorithm replacement**: The naive recursive approach makes exponential function calls (e.g., fibonacci(30) triggers over 2 million calls), while the fast-doubling method uses bit manipulation to traverse only the binary representation of n, requiring just log₂(n) iterations. 2. **Elimination of call stack overhead**: The recursive version creates deep call stacks with significant overhead for parameter passing and return value management. The iterative approach uses only local variables (a, b, c, d) with no function call overhead. 3. **Fast-doubling technique**: This algorithm leverages the mathematical property that F(2k) and F(2k+1) can be computed from F(k) and F(k+1) using simple arithmetic. By processing n's bits from most significant to least significant, it "doubles" the index at each step and conditionally adds 1, building up to F(n) efficiently. 4. **Cache-friendly computation**: Uses only primitive long operations with no object allocations, keeping all data in CPU registers for maximum performance. **Why This Optimization Matters:** The improvement is most dramatic for larger Fibonacci numbers. While both implementations handle small values (n ≤ 1) identically, the exponential growth of recursive calls makes the original version impractical for n > 40, whereas the optimized version handles even n = 90 (near the long overflow limit) in microseconds. The 30% speedup observed suggests the test cases include moderate-sized n values where the logarithmic advantage becomes significant while still being small enough that the recursive version completes in reasonable time. --- .../src/main/java/com/example/Algorithms.java | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/code_to_optimize/java/src/main/java/com/example/Algorithms.java b/code_to_optimize/java/src/main/java/com/example/Algorithms.java index bc976d3c3..af878b316 100644 --- a/code_to_optimize/java/src/main/java/com/example/Algorithms.java +++ b/code_to_optimize/java/src/main/java/com/example/Algorithms.java @@ -18,7 +18,26 @@ public long fibonacci(int n) { if (n <= 1) { return n; } - return fibonacci(n - 1) + fibonacci(n - 2); + // Use iterative fast-doubling method to compute F(n) in O(log n) time. + long a = 0L; // F(0) + long b = 1L; // F(1) + + int highestBit = 31 - Integer.numberOfLeadingZeros(n); + for (int i = highestBit; i >= 0; --i) { + // c = F(2k) = a * (2*b - a) + long c = a * ((b << 1) - a); + // d = F(2k+1) = a*a + b*b + long d = a * a + b * b; + + if (((n >> i) & 1) == 0) { + a = c; + b = d; + } else { + a = d; + b = c + d; + } + } + return a; } /**