Skip to content

Instantly share code, notes, and snippets.

@qolop
Last active May 27, 2016 19:29
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 qolop/bebb50ee539c0a6cebafc904c9807c5c to your computer and use it in GitHub Desktop.
Save qolop/bebb50ee539c0a6cebafc904c9807c5c to your computer and use it in GitHub Desktop.
Calculates factorial of a number.
[package]
name = "factorial"
version = "0.1.0"
authors = ["qolop <mptm98@gmail.com>"]
[dependencies]
num="*"
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