nums/
trial_division.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use alloc::vec;
use alloc::vec::Vec;
use num_bigint::BigUint;
use num_integer::Integer;
use num_traits::{One, Zero};

use crate::traits::PrimalityTest;
use crate::Factorizer;

/// The trial division method for primality testing or factorization.
pub struct TrialDivision;

impl PrimalityTest for TrialDivision {
    fn is_prime(&self, n: &BigUint) -> bool {
        if n.is_zero() || n.is_one() {
            return false;
        }

        let mut divisor = BigUint::from(2u8);
        while &divisor * &divisor <= *n {
            if n.is_multiple_of(&divisor) {
                return false;
            }
            divisor.inc();
        }
        true
    }
}

impl Factorizer for TrialDivision {
    fn factors(&self, n: &BigUint) -> Vec<BigUint> {
        assert!(!n.is_zero());

        let mut n = n.clone();
        let mut factors = vec![];
        let mut divisor = BigUint::from(2u8);

        while &divisor * &divisor <= n {
            let (quotient, remainder) = n.div_rem(&divisor);
            if remainder.is_zero() {
                factors.push(divisor.clone());
                n = quotient;
            } else {
                divisor.inc();
            }
        }

        if !n.is_one() {
            factors.push(n);
        }
        factors
    }
}

#[cfg(test)]
mod tests {
    use alloc::vec;
    use num_bigint::BigUint;
    use num_traits::{One, Zero};

    use crate::{Factorizer, PrimalityTest, TrialDivision};

    #[test]
    fn is_prime() {
        assert_eq!(TrialDivision.is_prime(&BigUint::zero()), false);
        assert_eq!(TrialDivision.is_prime(&BigUint::one()), false);
        assert_eq!(TrialDivision.is_prime(&BigUint::from(2u8)), true);
        assert_eq!(TrialDivision.is_prime(&BigUint::from(3u8)), true);
        assert_eq!(TrialDivision.is_prime(&BigUint::from(4u8)), false);
        assert_eq!(TrialDivision.is_prime(&BigUint::from(5u8)), true);
        assert_eq!(TrialDivision.is_prime(&BigUint::from(6u8)), false);
        assert_eq!(TrialDivision.is_prime(&BigUint::from(7u8)), true);
        assert_eq!(TrialDivision.is_prime(&BigUint::from(8u8)), false);
    }

    #[test]
    fn factor() {
        assert_eq!(TrialDivision.factors(&BigUint::one()), vec![]);
        assert_eq!(
            TrialDivision.factors(&BigUint::from(2u8)),
            vec![BigUint::from(2u8)]
        );
        assert_eq!(
            TrialDivision.factors(&BigUint::from(3u8)),
            vec![BigUint::from(3u8)]
        );
        assert_eq!(
            TrialDivision.factors(&BigUint::from(4u8)),
            vec![BigUint::from(2u8), BigUint::from(2u8)]
        );
        assert_eq!(
            TrialDivision.factors(&BigUint::from(5u8)),
            vec![BigUint::from(5u8)]
        );
        assert_eq!(
            TrialDivision.factors(&BigUint::from(6u8)),
            vec![BigUint::from(2u8), BigUint::from(3u8)]
        );
    }
}