-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathfactorial.rs
68 lines (61 loc) · 1.86 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use num_bigint::BigUint;
use num_traits::One;
#[allow(unused_imports)]
use std::str::FromStr;
pub fn factorial(number: u64) -> u64 {
// Base cases: 0! and 1! are both equal to 1
if number == 0 || number == 1 {
1
} else {
// Calculate factorial using the product of the range from 2 to the given number (inclusive)
(2..=number).product()
}
}
pub fn factorial_recursive(n: u64) -> u64 {
// Base cases: 0! and 1! are both equal to 1
if n == 0 || n == 1 {
1
} else {
// Calculate factorial recursively by multiplying the current number with factorial of (n - 1)
n * factorial_recursive(n - 1)
}
}
pub fn factorial_bigmath(num: u32) -> BigUint {
let mut result: BigUint = One::one();
for i in 1..=num {
result *= i;
}
result
}
// Module for tests
#[cfg(test)]
mod tests {
use super::*;
// Test cases for the iterative factorial function
#[test]
fn test_factorial() {
assert_eq!(factorial(0), 1);
assert_eq!(factorial(1), 1);
assert_eq!(factorial(6), 720);
assert_eq!(factorial(10), 3628800);
assert_eq!(factorial(20), 2432902008176640000);
}
// Test cases for the recursive factorial function
#[test]
fn test_factorial_recursive() {
assert_eq!(factorial_recursive(0), 1);
assert_eq!(factorial_recursive(1), 1);
assert_eq!(factorial_recursive(6), 720);
assert_eq!(factorial_recursive(10), 3628800);
assert_eq!(factorial_recursive(20), 2432902008176640000);
}
#[test]
fn basic_factorial() {
assert_eq!(factorial_bigmath(10), BigUint::from_str("3628800").unwrap());
assert_eq!(
factorial_bigmath(50),
BigUint::from_str("30414093201713378043612608166064768844377641568960512000000000000")
.unwrap()
);
}
}