Created
December 21, 2018 21:14
-
-
Save MightyPork/7b7319e5ff1d3ca91cf21f36340ca251 to your computer and use it in GitHub Desktop.
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::io; | |
use std::io::Write; | |
fn fibo(n:u32) -> u32 { | |
if n <= 2 { | |
1 | |
} else { | |
let mut prev1 = 1; | |
let mut prev2 = 1; | |
for _n in 2..n { | |
let next = prev1 + prev2; | |
prev2 = prev1; | |
prev1 = next; | |
} | |
prev1 | |
} | |
} | |
fn main() { | |
let mut s; | |
let num : u32 = loop { | |
print!("Enter a number: "); | |
io::stdout().flush() | |
.expect("Failed to flush stdout"); | |
s = String::new(); | |
io::stdin().read_line(&mut s) | |
.expect("Failed to read line"); | |
s = s.trim().to_string(); | |
if s.len() == 0 { | |
println!(""); | |
std::process::exit(1); | |
} | |
let _ : u32 = match s.parse() { | |
Ok(val) => { | |
break val; | |
}, | |
Err(_) => { | |
println!("Bad num: {}", s); | |
continue; | |
}, | |
}; | |
}; | |
println!("{}-th Fibonacci number is: {}", | |
num, | |
fibo(num) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment