Experiment D64F4ED: Reduce duration
Base 9fbd4a2 Metric ID bbf2b1a
rad-experiment show d64f4ed6edc8c09c0a76aa9ed248a3192f2c9e5d
by vagrant · Mar 31 14:13 2026
Fast doubling O(log n) with sum identity F(n+1)-1 — logarithmic time complexity
Delta+100.00%
Metricduration (ms)
Criterialower_is_better
LOC+16 -25
-010203040base123456789101112131415EXPERIMENTS
Kept Discarded Running best
Measurements
MetricBaselineCandidateDelta
duration (ms)How long the code takes to run. Less is better.38.310 ms (n=5)0.000 ms (n=5)+100.00%
ops_per_sec (ops/sec)Operations completed per second. More is better.26.100 ops/sec727272.240 ops/sec+2786383.67%
p95 (ms)Measures p95 in ms. Less is better.39.780 ms0.080 ms+99.79%
Diff
~ src/index.ts
@@ -1,39 +1,30 @@
1 /** 2 * Compute the sum of the first `n` Fibonacci numbers. 3 * 4+ * Uses the identity: sum(F(0)..F(n-1)) = F(n+1) - 1 5 * For n = 30, the expected result is 1,346,268. 6 */ 7 export function sumFibonacci(n: number): number { 7- const fibs: number[] = []; 8- 9- for (let i = 0; i < n; i++) { 10- // Pointless string round-trip on the index 11- const idx = parseInt(i.toString(), 10); 12- const value = fibonacci(idx); 13- fibs.push(value); 14- } 15- 16- // Reduce with unnecessary string conversions 17- const total = fibs.reduce((acc, cur) => { 18- return parseInt(acc.toString(), 10) + parseInt(cur.toString(), 10); 19- }, 0); 20- 21- return total; 8+ if (n <= 0) return 0; 9+ const [a] = fastDoubling(n + 1); 10+ return a - 1; 11 } 12 13 /** 25- * Compute the nth Fibonacci number using naive recursion. 14+ * Fast doubling: returns [F(n), F(n+1)] in O(log n) time. 15 * 27- * Deliberately avoids memoization, iterative approaches, or any 28- * optimization — resulting in O(2^n) time complexity. 16+ * F(2k) = F(k) * [2*F(k+1) - F(k)] 17+ * F(2k+1) = F(k)^2 + F(k+1)^2 18 */ 30-function fibonacci(n: number): number { 31- // Unnecessary string round-trip on every recursive call 32- const num = parseInt(n.toString(), 10); 19+function fastDoubling(n: number): [number, number] { 20+ if (n === 0) return [0, 1]; 21 34- if (num <= 0) return 0; 35- if (num === 1) return 1; 22+ const [a, b] = fastDoubling(Math.floor(n / 2)); 23+ const c = a * (2 * b - a); 24+ const d = a * a + b * b; 25 37- // Naive double recursion — no memoization, no caching 38- return fibonacci(num - 1) + fibonacci(num - 2); 26+ if (n % 2 === 0) { 27+ return [c, d]; 28+ } 29+ return [d, c + d]; 30 }
Environment
Base commit9fbd4a2599b8216c739e720cf014c3f890515ec7
Candidate commit571dae25b504410ce8f61689dd9058e7df9c77c1
Buildtrue
Teststrue
Metric IDbbf2b1a
Archaarch64
OSLinux 6.8.0-86-generic
CPU-
Agentclaude-code / claude-opus-4-6
Filessrc/index.ts