mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-06-06 01:27:27 +02:00
parent
0a1ddadc83
commit
cb59f30a2e
@ -4,16 +4,15 @@ minutes: 15
|
|||||||
|
|
||||||
# Exercise: Fibonacci
|
# Exercise: Fibonacci
|
||||||
|
|
||||||
The first and second Fibonacci numbers are both `1`. For n>2, the n'th Fibonacci
|
The Fibonacci sequence begins with `[0,1]`. For n>1, the n'th Fibonacci number
|
||||||
number is calculated recursively as the sum of the n-1'th and n-2'th Fibonacci
|
is calculated recursively as the sum of the n-1'th and n-2'th Fibonacci numbers.
|
||||||
numbers.
|
|
||||||
|
|
||||||
Write a function `fib(n)` that calculates the n'th Fibonacci number. When will
|
Write a function `fib(n)` that calculates the n'th Fibonacci number. When will
|
||||||
this function panic?
|
this function panic?
|
||||||
|
|
||||||
```rust,editable,should_panic
|
```rust,editable,should_panic
|
||||||
{{#include exercise.rs:fib}}
|
{{#include exercise.rs:fib}}
|
||||||
if n <= 2 {
|
if n < 2 {
|
||||||
// The base case.
|
// The base case.
|
||||||
todo!("Implement this")
|
todo!("Implement this")
|
||||||
} else {
|
} else {
|
||||||
|
@ -16,8 +16,8 @@
|
|||||||
// ANCHOR: fib
|
// ANCHOR: fib
|
||||||
fn fib(n: u32) -> u32 {
|
fn fib(n: u32) -> u32 {
|
||||||
// ANCHOR_END: fib
|
// ANCHOR_END: fib
|
||||||
if n <= 2 {
|
if n < 2 {
|
||||||
return 1;
|
return n;
|
||||||
} else {
|
} else {
|
||||||
return fib(n - 1) + fib(n - 2);
|
return fib(n - 1) + fib(n - 2);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user