Skip to content

Instantly share code, notes, and snippets.

@benja
Last active September 1, 2022 10:26
Show Gist options
  • Save benja/b4990c0a296d8d085d21695d568f2279 to your computer and use it in GitHub Desktop.
Save benja/b4990c0a296d8d085d21695d568f2279 to your computer and use it in GitHub Desktop.
Retrieve the nth value in the fibonacci sequence in Rust
// 0, 1, 1, 2, 3, 5, 8, 13, 21, 34
pub fn nth(n: usize) -> usize {
let mut v: Vec<usize> = vec![0, 1, 1];
for i in (2..n).skip(1) {
v.push(v[i - 1] + v[i - 2])
}
v[n - 1]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment