mirror of
https://github.com/google/comprehensive-rust.git
synced 2024-11-28 18:11:07 +02:00
parent
0a1ddadc83
commit
cb59f30a2e
@ -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 {
|
||||
|
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user