Last active
April 20, 2023 06:22
-
-
Save flameoftheforest/4ec8a773c184d0975aff5241c840bfdd to your computer and use it in GitHub Desktop.
Fibo in Rust
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
println!("{}", fib(5)); | |
println!("{}", fib(9)); | |
} | |
fn fib(n: i32) -> i32{ | |
let mut v = vec![n]; | |
loop { | |
v = v.iter().fold(vec![], |mut acc, _v| { | |
if *_v > 1 { | |
acc.push(*_v - 1); | |
acc.push(*_v - 2); | |
} else { | |
acc.push(*_v); | |
} | |
acc | |
}); | |
if v.iter().find(|_v| **_v > 1).is_none() { | |
return v.iter().fold(0, |acc, _v| { acc + *_v }); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment