Last active
January 1, 2019 23:48
-
-
Save pianomanfrazier/9a80f13c4a4476204cc3ce7ed45f2b8d to your computer and use it in GitHub Desktop.
A factorial test 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::cmp::Ordering; | |
use std::io; | |
const ONE: u64 = 1; | |
fn main() { | |
loop { | |
let mut guess = String::new(); | |
io::stdin() | |
.read_line(&mut guess) | |
.expect("Failed to read line"); | |
let guess: u64 = match guess.trim().parse() { | |
Ok(num) => num, | |
Err(_) => continue, | |
}; | |
println!("{}", fib(guess)); | |
} | |
} | |
fn fib(x: u64) -> u64 { | |
// match x.cmp(&ONE) { | |
// Ordering::Less => x, | |
// Ordering::Equal => x, | |
// Ordering::Greater => x * fib(x - 1), | |
// } | |
if let Ordering::Greater = x.cmp(&ONE) { | |
x * fib(x - 1) | |
} else { | |
x | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment