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:
ogaca42
pub fn fibonacci(n: u32) -> u32 { let mut values = vec![0, 1]; for i in 2..n + 1 { values.push(values[(i-1) as usize] + values[(i-2) as usize]); } values[n as usize]}
Maki-SIO
pub fn fibonacci(n: u32) -> u32 { if n <= 1 { return n; } let (mut a, mut b) = (0, 1); for _ in 2..=n { b = a + b; a = b - a; } b}
Mxn-ptr
pub fn fibonacci(n: u32) -> u32 { if n <= 1 { return n; } let (mut a, mut b) = (0, 1); for _ in 2..=n { b = a + b; a = b - a; } b}
ShileiShen
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence if 0 == n { return 0; } if 1 == n { return 1; } let mut a = 0; let mut b = 1; let mut c: u32; for _ in 2..=n { c = a + b; a = b; b = c; } b}
ShileiShen
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence if 0 == n { return 0; } if 1 == n { return 1; } fibonacci(n - 1) + fibonacci(n - 2)}
jamtg
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence match n { 0 => 0, 1 => 1, _ => fibonacci(n-1) + fibonacci(n-2) }}
yansq
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence match n { 0 => 0, 1 => 1, _ => fibonacci(n - 1) + fibonacci(n - 2) }}
funny233-github
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence match n { 0 => 0, 1 => 1, _ => fibonacci(n - 1) + fibonacci(n - 2) }}
zelsazgh
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) }}
jose-bernardo
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)}
digitalresistor
pub fn fibonacci(n: u32) -> u32 { if n == 0 { return 0; } if n == 1 { return 1; } fibonacci(n - 1) + fibonacci(n - 2)}
carlos-quantexai
pub fn fibonacci(n: u32) -> u32 { match n { 0 => 0, 1 => 1, _ => fibonacci(n - 1) + fibonacci(n - 2), // Recursive case }}
radloffl
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence match n { 0 => 0, 1 => 1, _ => fibonacci(n-1) + fibonacci(n-2) }}
retinotopic
pub fn fibonacci(n: u32) -> u32 { let mut slide1: u32 = 0; let mut slide2: u32 = 1; let mut temp: u32 = 0; for i in 0..n { temp = slide1; slide1 = slide2 + slide1; slide2 = temp; } return slide1;}
retinotopic
pub fn fibonacci(n: u32) -> u32 { let mut slide1: u32 = 0; let mut slide2: u32 = 1; let mut temp: u32 = 0; for i in 0..n { temp = slide1; slide1 = slide2 + slide1; slide2 = temp; } return slide1;}
jhq223
pub fn fibonacci(n: u32) -> u32 { if n == 0 { return 0; } if n == 1 { return 1; } return fibonacci(n - 1) + fibonacci(n - 2);}
xbarnett
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence let mut a = 0; let mut b = 1; for _ in 0..n { (a, b) = (b, a + b); } a}
mk-comm
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence if n == 0 { return 0 } else if n == 1 { return 1 } else { fibonacci(n-1) + fibonacci(n -2) }}
Johnchoi913
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence let mut count: u32 = 0; let (mut a, mut b) = (0, 1); while count < n { (a,b) = (b, a + b); count += 1; } a}
majesticalcreature
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence if n <= 1 { return n; } let mut prev = 0; let mut next = 1; let mut sum = 0; for _ in 0..n+1 { sum = prev + sum; prev = next; next = sum; } next}
LaurentiuStoleriu
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence let mut fibO: u32 = 0; let mut fibN: u32 = 1; let mut local_n = n; if local_n == 0 { return fibO; } if local_n == 1 { return fibN; } else { while local_n > 1 { fibN = fibN + fibO; fibO = fibN - fibO; local_n = local_n - 1; } return fibN; }}
matei
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence match n { 0 => 0, 1 => 1, _ => fibonacci(n - 1) + fibonacci(n - 2) }}
sander-b-postnl
pub fn fibonacci(n: u32) -> u32 { match n { 0 => 0, 1 => 1, _ => fibonacci(n - 1) + fibonacci(n - 2), }}
edoardo-morosanu
pub fn fibonacci(n: u32) -> u32 { match n { 0 => 0, 1 => 1, _ => fibonacci(n-1) + fibonacci(n-2), }}
i5-650
pub fn fibonacci(n: u32) -> u32 { if n == 0 { return 0; } let mut n1 = 0; let mut n2 = 1; let mut cn = n; for _ in 1..n { cn = n1 + n2; n1 = n2; n2 = cn; } cn}
amassare
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence let (mut a,mut b)=(0,1); for _ in 0..n as usize{ (a,b)=(b,a+b); } a}
alexromanov
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); }}
zavakid
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence fn go(n: u32, n1: u32, n2: u32) -> (u32, u32) { if n == 0 { (n1, n2) } else { go(n-1, n2, n1+n2) } } match n { 0 => 0, 1 => 1, _ => go(n - 1, 0, 1 ).1 } }
IdoPort
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence let mut n0 = 0; let mut n1 = 1; if n == 0 { return n0; } if n == 1 { return n1; } let mut fib = 0; for _ in 0..n-1 { fib = n0 + n1; n0 = n1; n1 = fib; } fib}
IdoPort
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence let mut n0 = 0; let mut n1 = 1; if n == 0 { return n0; } if n == 1 { return n1; } let mut fib = 0; for _ in 0..n-1 { fib = n0 + n1; n0 = n1; n1 = fib; } fib}
Ustin
pub fn fibonacci(n: u32) -> u32 { if n < 2 {return n}; fibonacci(n-2) + fibonacci(n-1)}
dylan-park
pub fn fibonacci(n: u32) -> u32 { if n < 2 { n } else { fibonacci(n - 2) + fibonacci(n - 1) }}
shankun
pub fn fibonacci(n: u32) -> u32 { if n < 2 { n } else { fibonacci(n - 2) + fibonacci(n - 1) }}
Parallaxes
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence match n { 0 => 0, 1 => 1, _ => fibonacci(n - 1) + fibonacci(n - 2), }}
Xinoi
pub fn fibonacci(n: u32) -> u32 { match n { 0 => 0, 1 => 1, _ => fibonacci(n-1) + fibonacci(n-2), }}
Aditeya
pub fn fibonacci(mut n: u32) -> u32 { // TODO: Implement the Fibonacci sequence if n == 0 { return 0; } if n > 0 && n < 3 { return 1; } let mut a = 0; let mut b = 1; n-=1; while n > 0 { let sum = a+b; a = b; b = sum; n -= 1; } return b;}
CianciuStyles
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence match n { 0 => 0, 1 => 1, _ => fibonacci(n-1) + fibonacci(n-2) }}
ayushrawat10
pub fn fibonacci(n: u32) -> u32 { if n <= 1 { return n } return fibonacci(n-1)+fibonacci(n-2)}
pbjarterot
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);}
masteryachty
pub fn fibonacci(n: u32) -> u32 { match n { 0 =>{ return 0; }, 1 => { return 1; }, _ =>{ return fibonacci(n-1) + fibonacci(n-2); } }}
damascussteel21
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence if n <= 1 { n } else { fibonacci(n - 1) + fibonacci(n - 2) }}
kyhou
pub fn fibonacci(n: u32) -> u32 { if n == 0u32 { return 0u32; }; if n == 1u32 { return 1u32; }; let mut prev: u32 = 0u32; let mut sum: u32 = 1u32; let mut new_sum: u32 = 0; for x in 2..=n { new_sum = sum + prev; prev = sum; sum = new_sum; }; new_sum as u32}
abhiyan-chhetri
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);}
oneopane
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence match n { 0 => 0, 1 => 1, _ => fibonacci(n - 1) + fibonacci(n - 2), }}
AtJiawei
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence match n { 0 => 0, 1 => 1, _ => fibonacci(n-1) + fibonacci(n-2) }}
edgarcnp
pub fn fibonacci(n: u32) -> u32 { (0..n).fold((0, 1), |(a, b), _| (b, a + b)).0}
Sommos
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence match n { 0 => 0, 1 => 1, _ => fibonacci(n - 1) + fibonacci(n - 2) }}
wischi-chr
pub fn fibonacci(n: u32) -> u32 { match n { 0 => 0, 1 => 1, _ => fibonacci(n-1) + fibonacci(n-2), }}
tukantje
pub fn fibonacci(n: u32) -> u32 { // TODO: Implement the Fibonacci sequence match n { 0 => 0, 1 => 1, _ => fibonacci(n - 1) + fibonacci(n - 2) }}
Ignition
pub fn fibonacci(n: u32) -> u32 { if n <= 1 { n } else { fibonacci(n - 1) + fibonacci(n - 2) }}