演習: フィボナッチ
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.
n 番目のフィボナッチ数を計算する関数 fib(n) を記述します。この関数はいつパニックするでしょうか。
fn fib(n: u32) -> u32 {
if n < 2 {
// ベースケース。
todo!("ここを実装してください")
} else {
// 再帰的なケース。
todo!("ここを実装してください")
}
}
fn main() {
let n = 20;
println!("fib({n}) = {}", fib(n));
}