Skip to content

Instantly share code, notes, and snippets.

@pianomanfrazier
Last active January 1, 2019 23:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pianomanfrazier/9a80f13c4a4476204cc3ce7ed45f2b8d to your computer and use it in GitHub Desktop.
Save pianomanfrazier/9a80f13c4a4476204cc3ce7ed45f2b8d to your computer and use it in GitHub Desktop.
A factorial test in Rust
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