Last active
September 1, 2022 10:26
-
-
Save benja/b4990c0a296d8d085d21695d568f2279 to your computer and use it in GitHub Desktop.
Retrieve the nth value in the fibonacci sequence 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
// 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