Skip to content

Instantly share code, notes, and snippets.

@flameoftheforest
Last active April 20, 2023 06:22
Show Gist options
  • Save flameoftheforest/4ec8a773c184d0975aff5241c840bfdd to your computer and use it in GitHub Desktop.
Save flameoftheforest/4ec8a773c184d0975aff5241c840bfdd to your computer and use it in GitHub Desktop.
Fibo in Rust
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