Created
August 10, 2019 18:40
-
-
Save sidoshi/73f514f9f001a786b623fe47d491fa25 to your computer and use it in GitHub Desktop.
Fibonacci in Rust
This file contains 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
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