1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2024-11-25 01:16:12 +02:00

Fix Fibonacci sequence i.e. fib(0) -> 0. (#1996)

related Issue #1985
This commit is contained in:
Jannis Imhof 2024-04-15 22:07:44 +02:00 committed by GitHub
parent 0a1ddadc83
commit cb59f30a2e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 5 additions and 6 deletions

View File

@ -4,16 +4,15 @@ minutes: 15
# Exercise: Fibonacci
The first and second Fibonacci numbers are both `1`. For n>2, the n'th Fibonacci
number is calculated recursively as the sum of the n-1'th and n-2'th Fibonacci
numbers.
The Fibonacci sequence begins with `[0,1]`. For n>1, the n'th Fibonacci number
is calculated recursively as the sum of the n-1'th and n-2'th Fibonacci numbers.
Write a function `fib(n)` that calculates the n'th Fibonacci number. When will
this function panic?
```rust,editable,should_panic
{{#include exercise.rs:fib}}
if n <= 2 {
if n < 2 {
// The base case.
todo!("Implement this")
} else {

View File

@ -16,8 +16,8 @@
// ANCHOR: fib
fn fib(n: u32) -> u32 {
// ANCHOR_END: fib
if n <= 2 {
return 1;
if n < 2 {
return n;
} else {
return fib(n - 1) + fib(n - 2);
}