The Fibonacci sequence is a series of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1. The sequence starts with 0 and 1, and then each number is the sum of the two preceding ones.
You are given a function fibonacci(n: u32) -> u32
that takes a number n
and returns the n
-th number in the Fibonacci sequence.
Your job is to implement the function meeting the following requirements:
n
is 0, the function should return 0.n
is 1, the function should return 1.n
is greater than 1, the function should return the sum of the two preceding numbers.Here are some examples of how the behavior of the function should look like:
kennethlove
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence if n == 0 { return 0 } else if n == 1 { return 1 } fibonacci(n-1) + fibonacci(n-2)}
tobiaszfilar
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence if n == 0 { return 0; } else if n == 1 { return 1; } else { return fibonacci(n - 1) + fibonacci(n - 2); }}
kylenoteboom
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence if n == 0 { return 0; } if n == 1 { return 1; } fibonacci(n -1) + fibonacci(n - 2)}
Nanle100
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence if n == 0 { return 0; } else if n == 1 { return 1; } else if n > 1 { return fibonacci(n - 1) + fibonacci(n - 2); } else { 0 }}
takes-ki
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence match n { 0 => 0, 1 => 1, _ => fibonacci(n-1) + fibonacci(n-2) } }
xenonminer
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence match n { 0 => 0, 1 => 1, _ => fibonacci(n-1) + fibonacci(n-2) }}
jimlawton
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence match n { 0 => 0, 1 => 1, _ => fibonacci(n - 1) + fibonacci(n - 2) }}
XtebanUy
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence let mut n_minus2 = 0; let mut n_minus1 = 1; match n { 0 => n_minus2, 1 => n_minus1, _ => { for _ in 2..=n { let swap = n_minus1; n_minus1 = n_minus1 + n_minus2; n_minus2 = swap; } n_minus1 } }}
5822791760
pub fn fibonacci(n: u32) -> u32 { if n <= 1 { return n; }; let mut a = 0; let mut b = 1; for _ in 2..n+1 { let temp = a + b; a = b; b = temp; } return b;}
mbergkvist
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence match n { 0 => 0, 1 => 1, _ => fibonacci(n-1) + fibonacci(n-2) }}
facat
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence // TODO: Implement the Fibonacci sequence match n { 0 => 0, 1 => 1, _ => fibonacci(n-1) + fibonacci(n-2), }}
DivineGod
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence match n { 0 => 0, 1 => 1, _ => fibonacci(n-1) + fibonacci(n-2), }}
StimhackSoftware
pub fn fibonacci(n: u32) -> u32 { if n > 1 { let mut n1 = 1; let mut n2 = 1; for _i in 2..n { let new = n1 + n2; n1 = n2; n2 = new; } n2 } else if n == 1{ 1 } else { 0 }}
mehdihmr
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence match n { 0 => 0, 1 => 1, _ => { return fibonacci(n - 1) + fibonacci(n - 2); } }}
mehdihmr
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence match n { 0 => 0, 1 => 1, _ => { return fibonacci(n - 1) + fibonacci(n - 2); } }}
MisterWoody
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence if n == 0 {return 0;} if n == 1 {return 1;} if n > 1 {return fibonacci (n - 1) + fibonacci (n - 2);} else {return 0;}}
Algorab
pub fn fibonacci(n: u32) -> u32 { if n == 0 { return 0; // Handle edge case for 0 terms } else if n == 1 { return 1; // Handle edge case for 1 term } let mut a = 0; let mut b = 1; for _ in 2..=n { let temp = a + b; a = b; b = temp; } b}
sroas
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence if n == 0 { return 0 } if n == 1 { return 1 } fibonacci(n - 1) + fibonacci(n - 2)}
felipebalbi
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence match n { 0 => 0, 1 => 1, k => fibonacci(k - 1) + fibonacci(k - 2), }}
xiuchiliudu
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence let mut num1 = 0; let mut num2 = 1; if n == 0 { return 0; } else if n == 1 { return 1; } for i in 2..=n { if i % 2 == 0 { num1 = num1 + num2; } else { num2 = num1 + num2; } } if n % 2 == 0 { num1 } else { num2 }}
danielmpetrov
pub fn fibonacci(n: u32) -> u32 { match n { 0 | 1 => n, _ => fibonacci(n - 1) + fibonacci(n - 2), }}
qiyuan711
pub fn fibonacci(n: u32) -> u32 { let mut a = 0; let mut b = 1; let mut count = n; if count == 0 { a } else if count == 1 { b } else { while count > 1 { let c = a + b; a = b; b = c; count -= 1 } b }}
jw
pub fn fibonacci(n: u32) -> u32 { match n { 0 => 0, 1 => 1, _ => fibonacci(n - 1) + fibonacci(n - 2) }}
yoakemae
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence if n == 0 || n == 1 { return n; } return fibonacci(n-2) + fibonacci(n-1);}
tinthid
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence let mut fib: Vec<u32> = vec![]; fib.extend(vec![0,1]); let mut i: usize = 2; while i <= n as usize{ fib.push(fib[i-2] + fib[i-1]); i += 1; } fib[n as usize]}
nickythorne
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence match n { 0 => return 0, 1 => return 1, _ => return fibonacci(n - 1) + fibonacci(n - 2) };}
DV-13
pub fn fibonacci(n: u32) -> u32 { if n < 2 { return n; } fibonacci(n-2) + fibonacci(n-1)}
joeslow
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence if n == 0 || n == 1 { n } else { fibonacci(n-1) + fibonacci(n-2) }}
RonaldoArayadev
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence if n == 0 { return 0; } else if n == 1 { return 1; } return fibonacci(n-1) + fibonacci(n-2);}
martin-unit
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence if n == 0 { return 0; } if n == 1 { return 1; } fibonacci(n-1) + fibonacci(n-2)}
Thymelizabeth
pub fn fibonacci(n: u32) -> u32 { if n <= 1 { n } else { fibonacci(n - 1) + fibonacci(n - 2) }}
konishu
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence if n == 0{ 0 } else if n == 1 { 1 } else { fibonacci( n - 1 ) + fibonacci(n - 2) } }
tamanishi
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence if n == 0 { return 0; } if n == 1 { return 1; } fibonacci(n - 1) + fibonacci(n - 2)}
devarajang
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence if n == 0 || n == 1 { return n; } else { return fibonacci(n-1) + fibonacci(n-2); }}
oDqnger
pub fn fibonacci(n: u32) -> u32 { if n == 0 { 0 } else { let mut list = vec![0,1]; for x in 2..n+1 { list.push(list[(x-2) as usize]+list[(x-1) as usize]); } list[list.len() - 1] }}
nichideropa
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence if n <= 1 { return n } fibonacci(n - 1) + fibonacci(n - 2)}
nichideropa
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence match n { 0 => 0, 1 => 1, _ => fibonacci(n - 2) + fibonacci(n - 1), }}
jeypiti
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence if n <= 1 { n } else { fibonacci(n-1) + fibonacci(n-2) }}
Sidhanta-Bkup
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence if n == 0 { return 0; } if n == 1 { return 1; } return fibonacci(n - 1) + fibonacci(n - 2) }
0xsmarter
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence match n { 0 => 0, 1 => 1, _ => fibonacci(n - 1) + fibonacci(n - 2), }}
t3stlab
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence match n { 0 => 0, 1 => 1, _ => fibonacci(n-1) + fibonacci(n-2), }}
aidan1magee
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence match n { 0 => 0, 1 => 1, _ => fibonacci(n - 1) + fibonacci(n - 2), }}
tsucchinoko
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence match n { 0 => 0, 1 => 1, _ => fibonacci(n - 1) + fibonacci(n - 2), }}
Kobeieii
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence let mut stack = vec![0, 1]; for i in 0..n+1 { if i > 1 { let fib_n = stack[(i-2) as usize] + stack[(i-1) as usize]; stack.push(fib_n); } } stack[n as usize]}
Kobeieii
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence let mut stack = vec![0, 1]; for i in 0..n+1 { if i > 1 { let fib_n = stack[(i-2) as usize] + stack[(i-1) as usize]; stack.push(fib_n); } } stack[n as usize]}
ankeetparikh
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence let mut a = 0; let mut b = 1; for _ in 1..=n { let tmp = b; b = a + b; a = tmp; } a}
maxvi
pub fn fibonacci(n: u32) -> u32 { match n { 0 => 0, 1 => 1, _ => fibonacci(n - 1) + fibonacci(n - 2), }}
gjmvervoort
pub fn fibonacci(n: u32) -> u32 { if n > 1 { fibonacci(n-1) + fibonacci(n-2) } else { n }}
LauriSarap
pub fn fibonacci(n: u32) -> u32 { match n { 0 => 0, 1 => 1, _ => fibonacci(n-1) + fibonacci(n-2), }}
nt2311-vn
pub fn fibonacci(n: u32) -> u32 { match n { 0 | 1 => n, _ => fibonacci(n-1) + fibonacci(n-2) }}