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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//use crate::Zero;
/// Represents the GCD (Greatest Common Divisor) trait.
/// This trait provides a method to calculate the GCD between two values of the same type.
impl_gcd!;
impl_gcd!;
impl_gcd!;
/*
/// Represents the GCD (Greatest Common Divisor) trait.
/// This trait provides a method to calculate the GCD between two values of the same type.
/// by default uses Euclidean algorithm to calculate GCD (or HCF) but for primitive numbers eg u8 .. i32...f64 Steins alogirthm is used
/// `TODO` : maybe add Rhs and output to make it more like add etc methods
pub trait Gcd/*<Rhs = Self>*/ : Zero + std::ops::Rem<Output = Self> + Sized + Copy {
/// Calculates the Greatest Common Divisor (GCD) between `self` and `other`.
///
/// For primitive number types like `i32` etc, the Stein's algorithm is used
/// to optimize the computation. For other types, the Euclidean algorithm is used by default if no custom implementation is given.
/// 'Note' : Find a way to make default implementation copyless
fn gcd(self, other: Self) -> Self {
if other.is_zero() {
self
} else {
(other % self).gcd(self)
}
}
}
macro_rules! impl_gcd {
(signed; $($t:ty),*) => {
$(
impl Gcd for $t {
fn gcd(self,other : Self) -> Self {
// Use Stein's algorithm
let mut m = self;
let mut n = other;
if m == 0 || n == 0 {
return (m | n).abs();
}
// find common factors of 2
let shift = (m | n).trailing_zeros();
// The algorithm needs positive numbers, but the minimum value
// can't be represented as a positive one.
// It's also a power of two, so the gcd can be
// calculated by bitshifting in that case
// Assuming two's complement, the number created by the shift
// is positive for all numbers except gcd = abs(min value)
// The call to .abs() causes a panic in debug mode
if m == Self::MIN || n == Self::MIN {
return ((1 << shift) as Self).abs();
}
// guaranteed to be positive now, rest like unsigned algorithm
m = m.abs();
n = n.abs();
// divide n and m by 2 until odd
m >>= m.trailing_zeros();
n >>= n.trailing_zeros();
while m != n {
if m > n {
m -= n;
m >>= m.trailing_zeros();
} else {
n -= m;
n >>= n.trailing_zeros();
}
}
m << shift
}
}
)*
};
(unsigned; $($t:ty),*) => {
$(
impl Gcd for $t {
fn gcd(self,other : Self) -> Self {
let mut m = self;
let mut n = other;
// find common factors of 2
let shift = (m | n).trailing_zeros();
// divide n and m by 2 until odd
m >>= m.trailing_zeros();
n >>= n.trailing_zeros();
while m != n {
if m > n {
m -= n;
m >>= m.trailing_zeros();
} else {
n -= m;
n >>= n.trailing_zeros();
}
}
m << shift
}
}
)*
};
(float; $($t:ty),*) => {
$(
impl Gcd for $t {}
)*
}
}
impl_gcd!(signed; i8,i16,i32,i64);
impl_gcd!(unsigned; u8,u16,u32,u64);
impl_gcd!(float; f32,f64);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_gcd_i8() {
let a: i8 = 15;
let b: i8 = 25;
let gcd_result = a.gcd(b);
assert_eq!(gcd_result, 5);
}
#[test]
fn test_gcd_i16() {
let a: i16 = 30;
let b: i16 = 45;
let gcd_result = a.gcd(b);
assert_eq!(gcd_result, 15);
}
#[test]
fn test_gcd_i32() {
let a: i32 = 80;
let b: i32 = 120;
let gcd_result = a.gcd(b);
assert_eq!(gcd_result, 40);
}
#[test]
fn test_gcd_i64() {
let a: i64 = 105;
let b: i64 = 140;
let gcd_result = a.gcd(b);
assert_eq!(gcd_result, 35);
}
#[test]
fn one_two(){
let a: i64 = 1;
let b: i64 = 2;
let gcd_result = a.gcd(b);
assert_eq!(gcd_result, 1);
}
}*/