-
Notifications
You must be signed in to change notification settings - Fork 0
/
factorial.rs
38 lines (34 loc) · 1.16 KB
/
factorial.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
extern crate num;
use std::from_str;
use std::io::BufferedReader;
use std::io;
use num::bigint::BigUint;
use std::num::One;
fn main() {
let mut reader = BufferedReader::new(io::stdin());
loop {
let input = reader.read_line().unwrap();
let arg = input.slice_to(input.len()-1);
println!("Input is {}.", arg);
if arg == "quit" {
println!("Received the 'quit' command.");
break;
}
match from_str::from_str(arg) {
Some(num) => { let n = FromPrimitive::from_u64(num);
match n {
Some(bignum) => println!("Factorial of {} is {}.", num,
recursive_factorial(&bignum).to_str()),
_ => println!("Num ({}), is not a uint.", num)
}
}
None => {println!("Argument must be a number.");}
};
}
}
fn recursive_factorial(n: &BigUint) -> ~BigUint {
match n {
n if n.eq(&One::one()) => ~One::one(),
_ => ~n.mul(recursive_factorial(&n.sub(&One::one())))
}
}