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
69
70
71
72
73
74
75
76
77
78
use num::PrimInt as Int;
use super::modulo;
#[inline]
pub fn is_factor<I>(num: I, query: I) -> bool
where I: Int {
modulo(num, query) == I::zero()
}
#[inline]
pub fn are_any_factors<I>(num: I, queries: &[I]) -> bool
where I: Int + Copy {
queries.iter().any(|query| is_factor(num, *query))
}
#[inline]
pub fn are_all_factors<I>(num: I, queries: &[I]) -> bool
where I: Int + Copy {
queries.iter().all(|query| is_factor(num, *query))
}
#[inline]
pub fn is_prime<I>(num: I) -> bool
where I: Int + Copy {
match get_all_factors(num).len() {
1 | 2 => true,
_ => false
}
}
#[inline]
pub fn lowest_non_one<I>(n: I) -> Option<I>
where I: Int + Copy {
let one: I = I::one();
let mut i: I = one + one;
while i * i <= n {
if n % i == I::zero() {
if i > one { return Some(i) }
else if i * i != n {
let n_div_i = n / i;
if n_div_i > one { return Some(n_div_i) }
}
}
i = i + one;
}
None
}
#[inline]
pub fn get_all_factors<I>(n: I) -> Vec<I> where I: Int {
let one: I = I::one();
let mut factors: Vec<I> = vec![one];
let mut i: I = one + one;
while i * i <= n {
if n % i == I::zero() {
factors.push(i);
if i * i != n { factors.push(n / i); }
}
i = i + one;
}
factors.push(n);
factors.sort_by(|a, b| a.cmp(b));
return factors;
}