Last active
May 27, 2016 19:29
-
-
Save qolop/bebb50ee539c0a6cebafc904c9807c5c to your computer and use it in GitHub Desktop.
Calculates factorial of a number.
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
[package] | |
name = "factorial" | |
version = "0.1.0" | |
authors = ["qolop <mptm98@gmail.com>"] | |
[dependencies] | |
num="*" |
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
extern crate num; | |
use num::{FromPrimitive, BigUint, One}; | |
use std::io; | |
fn main() { | |
println!("Please input the factorial."); | |
let mut s = String::new(); | |
io::stdin() | |
.read_line(&mut s) | |
.expect("failed to read line"); | |
let n: u16 = s.trim().parse().expect("Enter in a positive integer only"); | |
println!("Factorial of {} = {}", n, factorial(n)); | |
} | |
fn factorial(n: u16) -> BigUint { | |
let mut f: BigUint = One::one(); | |
for i in 1..n+1 { | |
let bu: BigUint = FromPrimitive::from_u16(i).unwrap(); | |
f = f * bu; | |
} | |
f | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment