Skip to content

feat(maths): recursive calculating of Fibonacci numbers #107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 9, 2023
Merged

feat(maths): recursive calculating of Fibonacci numbers #107

merged 4 commits into from
Mar 9, 2023

Conversation

zFlxw
Copy link
Contributor

@zFlxw zFlxw commented Mar 5, 2023

This is an improvement to the algorithm for calculating the nth Fibonacci number, as the algorithm can be implemented recursively. Also, I have improved and structured the tests a bit different.

Feel free to share your thoughts and give feedback.

Copy link
Contributor

@appgurueu appgurueu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an improvement to the algorithm for calculating the nth Fibonacci number, as the algorithm can be implemented recursively.

No, the recursive implementation is not at all an improvement. It is much, much worse runtime-wise - the "dynamic programming" algorithm that was previously implemented runs in linear time, whereas this runs in exponential time.

You may still add the recursive version (if you also add a big warning), but you may not replace the preferable dynamic programming version.

The improvements to the tests are fine.

@zFlxw
Copy link
Contributor Author

zFlxw commented Mar 5, 2023

Alright, thank you for your feedback. So may I convert my changes to an additional closure and keep the original one also?

@zFlxw zFlxw requested review from appgurueu and removed request for raklaptudirm March 5, 2023 13:36
Copy link
Contributor

@appgurueu appgurueu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is fine, but please don't duplicate the tests: Just parameterize the tests in terms of the function to be tested, then run once for the iterative, once for the recursive variant.

@zFlxw
Copy link
Contributor Author

zFlxw commented Mar 5, 2023

I am pretty new to writing tests in JS/TS, but hopefully I understood correctly what you meant. If not, please give feedback about the tests again. :)

@zFlxw zFlxw requested a review from appgurueu March 5, 2023 14:53
Copy link
Contributor

@appgurueu appgurueu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I had in mind when I said "parameterize the tests in terms of the function" was rather the following:

const test = func => it.each([[0, 0], [1, 1], [2, 1], [5, 5], [10, 55], [15, 610]])('fib(%i) = %i', (n, expected) => expect(func(n)).toBe(expected));
describe("Fibonacci iterative", () => test(nthFibonacci));
describe("Fibonacci recursive", () => test(nthFibonacciRecursively));

@zFlxw
Copy link
Contributor Author

zFlxw commented Mar 5, 2023

Thanks for clarifying, I have adjusted the tests.

@zFlxw zFlxw requested a review from appgurueu March 5, 2023 17:10
@raklaptudirm raklaptudirm merged commit 8e83941 into TheAlgorithms:master Mar 9, 2023
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.

3 participants