johnston/
math.rs

1fn factors(num: i32) -> Vec<i32> {
2    let mut result: Vec<i32> = Vec::new();
3
4    for n in 1..=num {
5        if num % n == 0 {
6            result.push(n);
7        }
8    }
9
10    result
11}
12
13pub(crate) fn prime(num: &i32) -> bool {
14    for n in 2..*num {
15        if num % n == 0 {
16            return false;
17        }
18    }
19
20    true
21}
22
23pub(crate) fn gcd(x: i32, y: i32) -> i32 {
24    let mut x = x;
25    let mut y = y;
26    while y != 0 {
27        let t = y;
28        y = x % y;
29        x = t;
30    }
31    x
32}
33
34/// Returns the greatest prime factor of a number.
35pub(crate) fn gpf(num: i32) -> i32 {
36    let mut result = 0;
37
38    for n in factors(num) {
39        if prime(&n) && n > result {
40            result = n;
41        }
42    }
43
44    result
45}
46
47/// Returns `true` if the input isa power of two.
48pub(crate) fn power_of_two(num: i32) -> bool {
49    num != 0 && (num & (num - 1)) == 0
50}