-
-
Notifications
You must be signed in to change notification settings - Fork 409
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
feat(maths): recursive calculating of Fibonacci numbers #107
Conversation
There was a problem hiding this 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.
Alright, thank you for your feedback. So may I convert my changes to an additional closure and keep the original one also? |
There was a problem hiding this 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.
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. :) |
There was a problem hiding this 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));
Thanks for clarifying, I have adjusted the tests. |
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.