ya_advent_lib/
math.rs

1pub fn firstfac(x: u64) -> u64 {
2    if x % 2 == 0 {
3        return 2;
4    };
5    for n in (3..).step_by(2).take_while(|m| m * m <= x) {
6        if x % n == 0 {
7            return n;
8        };
9    }
10    x
11}
12
13pub fn is_prime(x: u64) -> bool {
14    match x {
15        0 | 1 => false,
16        2 => true,
17        n => firstfac(n) == n,
18    }
19}
20
21pub fn prime_factors(x: u64) -> Vec<u64> {
22    if x <= 1 {
23        return vec![];
24    };
25    let mut lst: Vec<u64> = Vec::new();
26    let mut current = x;
27    loop {
28        let m = firstfac(current);
29        lst.push(m);
30        if current == m {
31            break;
32        }
33        while current % m == 0 {
34            current /= m;
35        }
36        if current == 1 {
37            break;
38        }
39    }
40    lst
41}