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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
use crate::natural::Natural;
#[cfg(feature = "test_build")]
use malachite_base::num::arithmetic::traits::DivisibleBy;
use malachite_base::num::arithmetic::traits::{CoprimeWith, Gcd, Parity};

pub_test! {coprime_with_check_2(x: Natural, y: Natural) -> bool {
    (x.odd() || y.odd()) && x.gcd(y) == 1
}}

#[cfg(feature = "test_build")]
pub fn coprime_with_check_2_3(x: Natural, y: Natural) -> bool {
    (x.odd() || y.odd())
        && (!(&x).divisible_by(Natural::from(3u32)) || !(&y).divisible_by(Natural::from(3u32)))
        && x.gcd(y) == 1
}

#[cfg(feature = "test_build")]
pub fn coprime_with_check_2_3_5(x: Natural, y: Natural) -> bool {
    if x.even() && y.even() {
        false
    } else {
        let x15 = &x % Natural::from(15u32);
        let y15 = &y % Natural::from(15u32);
        if (x15 == 0 || x15 == 3 || x15 == 6 || x15 == 9 || x15 == 12)
            && (y15 == 0 || y15 == 3 || y15 == 6 || y15 == 9 || y15 == 12)
        {
            return false;
        }
        if (x15 == 0 || x15 == 5 || x15 == 10) && (y15 == 0 || y15 == 5 || y15 == 10) {
            return false;
        }
        x.gcd(y) == 1
    }
}

pub_test! {coprime_with_check_2_val_ref(x: Natural, y: &Natural) -> bool {
    (x.odd() || y.odd()) && x.gcd(y) == 1
}}

pub_test! {coprime_with_check_2_ref_val(x: &Natural, y: Natural) -> bool {
    (x.odd() || y.odd()) && x.gcd(y) == 1
}}

pub_test! {coprime_with_check_2_ref_ref(x: &Natural, y: &Natural) -> bool {
    (x.odd() || y.odd()) && x.gcd(y) == 1
}}

impl CoprimeWith<Natural> for Natural {
    /// Returns whether two [`Natural`]s are coprime; that is, whether they have no common factor
    /// other than 1. Both [`Natural`]s are taken by value.
    ///
    /// Every [`Natural`] is coprime with 1. No [`Natural`] is coprime with 0, except 1.
    ///
    /// $f(x, y) = (\gcd(x, y) = 1)$.
    ///
    /// $f(x, y) = ((k,m,n \in \N \land x=km \land y=kn) \implies k=1)$.
    ///
    /// # Worst-case complexity
    /// $T(n) = O(n (\log n)^2 \log\log n)$
    ///
    /// $M(n) = O(n \log n)$
    ///
    /// where $T$ is time, $M$ is additional memory, and $n$ is
    /// `max(self.significant_bits(), other.significant_bits())`.
    ///
    /// # Examples
    /// ```
    /// extern crate malachite_base;
    ///
    /// use malachite_base::num::arithmetic::traits::CoprimeWith;
    /// use malachite_nz::natural::Natural;
    ///
    /// assert_eq!(Natural::from(3u32).coprime_with(Natural::from(5u32)), true);
    /// assert_eq!(Natural::from(12u32).coprime_with(Natural::from(90u32)), false);
    /// ```
    #[inline]
    fn coprime_with(self, other: Natural) -> bool {
        coprime_with_check_2(self, other)
    }
}

impl<'a> CoprimeWith<&'a Natural> for Natural {
    /// Returns whether two [`Natural`]s are coprime; that is, whether they have no common factor
    /// other than 1. The first [`Natural`] is taken by value and the second by reference.
    ///
    /// Every [`Natural`] is coprime with 1. No [`Natural`] is coprime with 0, except 1.
    ///
    /// $f(x, y) = (\gcd(x, y) = 1)$.
    ///
    /// $f(x, y) = ((k,m,n \in \N \land x=km \land y=kn) \implies k=1)$.
    ///
    /// # Worst-case complexity
    /// $T(n) = O(n (\log n)^2 \log\log n)$
    ///
    /// $M(n) = O(n \log n)$
    ///
    /// where $T$ is time, $M$ is additional memory, and $n$ is
    /// `max(self.significant_bits(), other.significant_bits())`.
    ///
    /// # Examples
    /// ```
    /// extern crate malachite_base;
    ///
    /// use malachite_base::num::arithmetic::traits::CoprimeWith;
    /// use malachite_nz::natural::Natural;
    ///
    /// assert_eq!(Natural::from(3u32).coprime_with(&Natural::from(5u32)), true);
    /// assert_eq!(Natural::from(12u32).coprime_with(&Natural::from(90u32)), false);
    /// ```
    #[inline]
    fn coprime_with(self, other: &'a Natural) -> bool {
        coprime_with_check_2_val_ref(self, other)
    }
}

impl<'a> CoprimeWith<Natural> for &'a Natural {
    /// Returns whether two [`Natural`]s are coprime; that is, whether they have no common factor
    /// other than 1. The first [`Natural`] is taken by reference and the second by value.
    ///
    /// Every [`Natural`] is coprime with 1. No [`Natural`] is coprime with 0, except 1.
    ///
    /// $f(x, y) = (\gcd(x, y) = 1)$.
    ///
    /// $f(x, y) = ((k,m,n \in \N \land x=km \land y=kn) \implies k=1)$.
    ///
    /// # Worst-case complexity
    /// $T(n) = O(n (\log n)^2 \log\log n)$
    ///
    /// $M(n) = O(n \log n)$
    ///
    /// where $T$ is time, $M$ is additional memory, and $n$ is
    /// `max(self.significant_bits(), other.significant_bits())`.
    ///
    /// # Examples
    /// ```
    /// extern crate malachite_base;
    ///
    /// use malachite_base::num::arithmetic::traits::CoprimeWith;
    /// use malachite_nz::natural::Natural;
    ///
    /// assert_eq!((&Natural::from(3u32)).coprime_with(Natural::from(5u32)), true);
    /// assert_eq!((&Natural::from(12u32)).coprime_with(Natural::from(90u32)), false);
    /// ```
    #[inline]
    fn coprime_with(self, other: Natural) -> bool {
        coprime_with_check_2_ref_val(self, other)
    }
}

impl<'a, 'b> CoprimeWith<&'b Natural> for &'a Natural {
    /// Returns whether two [`Natural`]s are coprime; that is, whether they have no common factor
    /// other than 1. Both [`Natural`]s are taken by reference.
    ///
    /// Every [`Natural`] is coprime with 1. No [`Natural`] is coprime with 0, except 1.
    ///
    /// $f(x, y) = (\gcd(x, y) = 1)$.
    ///
    /// $f(x, y) = ((k,m,n \in \N \land x=km \land y=kn) \implies k=1)$.
    ///
    /// # Worst-case complexity
    /// $T(n) = O(n (\log n)^2 \log\log n)$
    ///
    /// $M(n) = O(n \log n)$
    ///
    /// where $T$ is time, $M$ is additional memory, and $n$ is
    /// `max(self.significant_bits(), other.significant_bits())`.
    ///
    /// # Examples
    /// ```
    /// extern crate malachite_base;
    ///
    /// use malachite_base::num::arithmetic::traits::CoprimeWith;
    /// use malachite_nz::natural::Natural;
    ///
    /// assert_eq!((&Natural::from(3u32)).coprime_with(Natural::from(5u32)), true);
    /// assert_eq!((&Natural::from(12u32)).coprime_with(Natural::from(90u32)), false);
    /// ```
    fn coprime_with(self, other: &'b Natural) -> bool {
        coprime_with_check_2_ref_ref(self, other)
    }
}