Skip to content

Instantly share code, notes, and snippets.

@sidoshi
Created August 10, 2019 18:40
Show Gist options
  • Save sidoshi/73f514f9f001a786b623fe47d491fa25 to your computer and use it in GitHub Desktop.
Save sidoshi/73f514f9f001a786b623fe47d491fa25 to your computer and use it in GitHub Desktop.
Fibonacci in Rust
use std::env;
use num_bigint::{BigUint, ToBigUint};
fn fib(n: usize) -> BigUint {
let mut dp = Vec::with_capacity(n+1);
dp.push(0.to_biguint().unwrap());
dp.push(1.to_biguint().unwrap());
for i in 2..=n {
dp.push(&dp[i - 1] + &dp[i - 2]);
}
dp[n].clone()
}
fn main() {
let args: Vec<String> = env::args().collect();
let n: usize = args[1]
.parse()
.expect("Please enter a valid number!");
println!("{}", fib(n));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment