From cb59f30a2e1cda87b8836793b1285fe37dda05ab Mon Sep 17 00:00:00 2001 From: Jannis Imhof <125810509+withjannis@users.noreply.github.com> Date: Mon, 15 Apr 2024 22:07:44 +0200 Subject: [PATCH] Fix Fibonacci sequence i.e. fib(0) -> 0. (#1996) related Issue #1985 --- src/types-and-values/exercise.md | 7 +++---- src/types-and-values/exercise.rs | 4 ++-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/types-and-values/exercise.md b/src/types-and-values/exercise.md index 1512208b..f12aec7c 100644 --- a/src/types-and-values/exercise.md +++ b/src/types-and-values/exercise.md @@ -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 { diff --git a/src/types-and-values/exercise.rs b/src/types-and-values/exercise.rs index f09513f4..01a8ad4a 100644 --- a/src/types-and-values/exercise.rs +++ b/src/types-and-values/exercise.rs @@ -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); }