Mistake in Fibonacci Exercise
withjannis opened this issue · comments
The course includes an exercise using the Fibonacci sequence as an example.
In my opinion the implementation is wrong.
The Fibonacci sequence starts with 0, 1, 1. [1]
In the files mentioned, the code implies that the sequence starts with 1, 1, 1.
The error is in the following files:
src/types-and-values/exercise.rs
src/types-and-values/exercise.md
// src/types-and-values/exercise.rs
fn fib(n: u32) -> u32 {
if n <= 2 {
return 1;
} else {
return fib(n - 1) + fib(n - 2);
}
}
Using this algorithm, this would be the output:
fib(0); // output: 1, IS WRONG
fib(1); // output: 1
fib(1); // output: 1
This fix would correct the implementation of the code.
fn fib(n: u32) -> u32 {
if n < 2 {
return n;
} else {
return fib(n - 1) + fib(n - 2);
}
}
[1] https://en.wikipedia.org/wiki/Fibonacci_sequence
Disclaimer: This is my first issue, I would appreciate feedback.
Thanks for the issue!
We addressed some similar questions in #1529. I think in this case we're implementing what the "older" form, where F1 and F2 are both 1 and the sequence precedes from there (with F0 unspecified). For purposes of understanding Rust, I don't think it makes much difference.
But, since this is the second time this has come up and since the Wikipedia article does describe this form as "older", I'd be happy to merge a PR fixing both the problem statement and the solution.
Thanks @withjannis for fixing this! Could I get you to add a reference to https://oeis.org/A000045 in a comment? That should help keep this stable going forward since that sequence is well-defined.
I can't quite follow you @mgeisler, sorry.
Where would you like to add a comment referring to https://oeis.org/A000045, in src/types-and-values/exercise.rs
?
Yes, exactly! Please add it as a speaker note for future reference for people who view/edit the page.
I think we can close this bug now since the sequence is fixed — a speaker note can be added later, if people think it would be helpful.