-
Notifications
You must be signed in to change notification settings - Fork 9
/
lib.rs
33 lines (31 loc) · 846 Bytes
/
lib.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
use P35::PrimeIterator;
pub fn prime_factor_multiplicity(n: u32) -> Vec<(u32, u32)> {
let piter = PrimeIterator::new();
let mut factors = vec![];
let mut r = n;
for prime in piter {
if r == 1 {
break;
}
let mut c = 0;
while r % prime == 0 {
c += 1;
r = r / prime;
}
if c > 0 {
factors.push((prime, c));
}
}
factors
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_prime_factor_multiplicity() {
assert_eq!(prime_factor_multiplicity(9), vec![(3, 2)]);
assert_eq!(prime_factor_multiplicity(20), vec![(2, 2), (5, 1)]);
assert_eq!(prime_factor_multiplicity(111), vec![(3, 1), (37, 1)]);
assert_eq!(prime_factor_multiplicity(315), vec![(3, 2), (5, 1), (7, 1)]);
}
}