Skip to content

fix: isolate Fibonacci sequence state and handle short sequences - #2032

Open
lanqing431 wants to merge 1 commit into
TheAlgorithms:masterfrom
lanqing431:fix/fibonacci-sequence-calls
Open

lanqing431 wants to merge 1 commit into
TheAlgorithms:masterfrom
lanqing431:fix/fibonacci-sequence-calls

Conversation

@lanqing431

Copy link
Copy Markdown

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

FibonacciRecursive(6) followed by FibonacciRecursive(2) overflows the call stack. Its module-level sequence already exceeds the requested length, so the equality stop condition is never reached. Index zero also fails. The iterative DP variant always starts with three entries, even when only index zero or one is requested.

Keep the recursive sequence local to each invocation and initialize both sequence implementations to the requested boundary. Existing negative-index conventions remain unchanged. Five new regression cases cover decreasing/repeated requests, zero and one, including negative indices.

Validation (Node.js 24.22.0): all five new cases fail on unchanged master. npm test -- --maxWorkers=2 --minWorkers=1 passes all 11975 tests in 346 files. npm run check-style and git diff --check pass.

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is my own contribution and does not copy another proposed fix.
  • This PR only changes one algorithm file and its tests.
  • All files remain in their existing directories.

Prepared with OpenAI Codex assistance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant