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 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
#![cfg_attr(not(test), no_std)] //! ModNum is a highly ergonomic modular arithmetic struct intended for no_std use. //! //! ModNum objects represent a value modulo m. The value and modulo can be of any //! primitive integer type. Arithmetic operators include +, - (both unary and binary), //! *, and ==. //! //! ModNum was originally developed to facilitate bidirectional navigation through fixed-size //! arrays at arbitrary starting points. This is facilitated by a double-ended iterator that //! traverses the entire ring starting at any desired value. //! //! Note that ModNum is not designed to work with arbitrary-length integers, as it requires its //! integer type to implement the Copy trait. //! //! For the [2020 Advent of Code](https://adventofcode.com/2020) //! ([Day 13](https://adventofcode.com/2020/day/13) part 2), //! I extended ModNum to solve systems of modular equations, provided that each modular equation //! is represented using signed integers. My implementation is based on this //! [lucid](https://byorgey.wordpress.com/2020/02/15/competitive-programming-in-haskell-modular-arithmetic-part-1/) //! [explanation](https://byorgey.wordpress.com/2020/03/03/competitive-programming-in-haskell-modular-arithmetic-part-2/) //! by [Brent Yorgey](http://ozark.hendrix.edu/~yorgey/). //! //! # Arithmetic Examples //! Addition, subtraction, multiplication, and unary negation are all fully supported for both //! signed and unsigned integer types. Note that the right-hand side will be an integer of the //! corresponding type, rather than another ModNum. I have personally found this to be most //! convenient in practice. //! //! ``` //! use bare_metal_modulo::ModNum; //! let mut m = ModNum::new(2, 5); //! m += 2; //! assert_eq!(m, ModNum::new(4, 5)); //! m += 2; //! assert_eq!(m, ModNum::new(1, 5)); //! m -= 3; //! assert_eq!(m, ModNum::new(3, 5)); //! m *= 2; //! assert_eq!(m, ModNum::new(1, 5)); //! m *= 2; //! assert_eq!(m, ModNum::new(2, 5)); //! m *= 2; //! assert_eq!(m, ModNum::new(4, 5)); //! m *= 2; //! assert_eq!(m, ModNum::new(3, 5)); //! m = -m; //! assert_eq!(m, ModNum::new(2, 5)); //! assert_eq!(m.a(), 2); //! assert_eq!(m.m(), 5); //! ``` //! //! The **==** operator can be used to compare two ModNums or a ModNum and an //! integer of the corresponding type. In both cases, it represents congruence rather than //! strict equality. //! //! ``` //! use bare_metal_modulo::ModNum; //! let m = ModNum::new(2, 5); //! assert!(m == 2); //! assert!(m == 7); //! assert!(m == -3); //! ``` //! //! # Accessing Values //! Each ModNum represents an integer **a (mod m)**. To access these values, use the //! corresponding **a()** and **m()** methods. Note that **a()** will always return a fully //! reduced value, regardless of how it was initialized. //! //! ``` //! use bare_metal_modulo::ModNum; //! let m = ModNum::new(7, 10); //! assert_eq!(m.a(), 7); //! assert_eq!(m.m(), 10); //! //! let n = ModNum::new(23, 17); //! assert_eq!(n.a(), 6); //! assert_eq!(n.m(), 17); //! //! let p = ModNum::new(-4, 3); //! assert_eq!(p.a(), 2); //! assert_eq!(p.m(), 3); //! ``` //! //! # Iteration //! I originally created ModNum to facilitate cyclic iteration through a fixed-size array from an //! arbitrary starting point in a no_std environment. Its double-ended iterator facilitates //! both forward and backward iteration. //! //! ``` //! use bare_metal_modulo::ModNum; //! //! let forward: Vec<usize> = ModNum::new(2, 5).iter().map(|mn| mn.a()).collect(); //! assert_eq!(forward, vec![2, 3, 4, 0, 1]); //! //! let reverse: Vec<usize> = ModNum::new(2, 5).iter().rev().map(|mn| mn.a()).collect(); //! assert_eq!(reverse, vec![1, 0, 4, 3, 2]); //! ``` //! //! For the [2020 Advent of Code](https://adventofcode.com/2020) //! ([Day 13](https://adventofcode.com/2020/day/13) part 2), //! I extended ModNum to solve systems of modular equations, provided that each modular equation //! is represented using signed integers. My implementation is based on this //! [lucid](https://byorgey.wordpress.com/2020/02/15/competitive-programming-in-haskell-modular-arithmetic-part-1/) //! [explanation](https://byorgey.wordpress.com/2020/03/03/competitive-programming-in-haskell-modular-arithmetic-part-2/) //! by [Brent Yorgey](http://ozark.hendrix.edu/~yorgey/). //! //! The solver works directly with an iterator containing the ModNum objects corresponding to the //! modular equations to be solved, facilitating space-efficient solutions of a sequence coming //! from a stream. The examples below show two variants of the same system. The first example uses //! an iterator, and the second example retrieves the system from a Vec. //! //! ``` //! use bare_metal_modulo::ModNum; //! let a_values = (2..=4); //! let m_values = (5..).step_by(2); //! let mut values = a_values.zip(m_values).map(|(a, m)| ModNum::new(a, m)); //! let solution = ModNum::<i128>::chinese_remainder_system(&mut values).unwrap(); //! assert_eq!(solution, 157); //! //! let values = vec![ModNum::new(2, 5), ModNum::new(3, 7), ModNum::new(4, 9)]; //! let solution = ModNum::<i128>::chinese_remainder_system(&mut values.iter().copied()).unwrap(); //! assert_eq!(solution, 157); //! ``` use core::mem; use num::{Integer, Signed}; use core::ops::{Add, AddAssign, Sub, SubAssign, Mul, MulAssign, Neg}; /// Represents an integer **a (mod m)** #[derive(Debug,Copy,Clone,Eq,PartialEq,Ord,PartialOrd)] pub struct ModNum<N> { num: N, modulo: N } impl <N: Integer+Copy> ModNum<N> { /// Creates a new integer **a (mod m)** pub fn new(a: N, m: N) -> Self { ModNum { num: a.mod_floor(&m), modulo: m } } /// Returns the integer value **a** for **a (mod m)** pub fn a(&self) -> N { self.num } /// Returns the modulo value **m** for **a (mod m)** pub fn m(&self) -> N { self.modulo } /// Returns an iterator starting at **a (mod m)** and ending at **a - 1 (mod m)** pub fn iter(&self) -> ModNumIterator<N> { ModNumIterator::new(*self) } } impl <N: Integer + Signed + Copy> ModNum<N> { pub fn chinese_remainder(&self, other: ModNum<N>) -> ModNum<N> { let (g, u, v) = ModNum::egcd(self.modulo, other.modulo); let c = (self.num * other.modulo * v + other.num * self.modulo * u) / g; ModNum::new(c, self.modulo * other.modulo) } pub fn chinese_remainder_system<I:Iterator<Item=ModNum<N>>>(modnums: &mut I) -> Option<ModNum<N>> { modnums.next().map(|start_num| modnums.fold(start_num, |a, b| a.chinese_remainder(b))) } pub fn egcd(a: N, b: N) -> (N,N,N) { if b == N::zero() { (a.signum() * a, a.signum(), N::zero()) } else { let (g, x, y) = ModNum::egcd(b, a.mod_floor(&b)); (g, y, x - (a / b) * y) } } } // Congruence impl <N:Integer+Copy> PartialEq<N> for ModNum<N> { fn eq(&self, other: &N) -> bool { self.num == other.mod_floor(&self.modulo) } } impl <N:Integer+Copy> Add<N> for ModNum<N> { type Output = ModNum<N>; fn add(self, rhs: N) -> Self::Output { ModNum::new(self.num + rhs, self.modulo) } } impl <N:Integer+Copy> AddAssign<N> for ModNum<N> { fn add_assign(&mut self, rhs: N) { *self = *self + rhs; } } impl <N:Integer+Copy> Neg for ModNum<N> { type Output = ModNum<N>; fn neg(self) -> Self::Output { ModNum::new(self.modulo - self.num, self.modulo) } } impl <N:Integer+Copy> Sub<N> for ModNum<N> { type Output = ModNum<N>; fn sub(self, rhs: N) -> Self::Output { self + (-ModNum::new(rhs, self.modulo)).a() } } impl <N:Integer+Copy> SubAssign<N> for ModNum<N> { fn sub_assign(&mut self, rhs: N) { *self = *self - rhs; } } impl <N:Integer+Copy> Mul<N> for ModNum<N> { type Output = ModNum<N>; fn mul(self, rhs: N) -> Self::Output { ModNum::new(self.num * rhs, self.modulo) } } impl <N:Integer+Copy> MulAssign<N> for ModNum<N> { fn mul_assign(&mut self, rhs: N) { *self = *self * rhs; } } #[derive(Debug)] pub struct ModNumIterator<N> { next: ModNum<N>, next_back: ModNum<N>, finished: bool } impl <N: Integer+Copy> ModNumIterator<N> { pub fn new(mn: ModNum<N>) -> Self { ModNumIterator {next: mn, next_back: mn - N::one(), finished: false} } } fn update<N: Integer+Copy, F:Fn(&ModNum<N>,N)->ModNum<N>>(finished: &mut bool, update: &mut ModNum<N>, updater: F, target: ModNum<N>) -> Option<<ModNumIterator<N> as Iterator>::Item> { if *finished { None } else { let mut future = updater(update, N::one()); if future == updater(&target, N::one()) { *finished = true; } mem::swap(&mut future, update); Some(future) } } impl <N: Integer+Copy> Iterator for ModNumIterator<N> { type Item = ModNum<N>; fn next(&mut self) -> Option<Self::Item> { update(&mut self.finished, &mut self.next, |m, u| *m + u, self.next_back) } } impl <N: Integer+Copy> DoubleEndedIterator for ModNumIterator<N> { fn next_back(&mut self) -> Option<Self::Item> { update(&mut self.finished, &mut self.next_back, |m, u| *m - u, self.next) } } #[cfg(test)] mod tests { use super::*; #[test] fn test_neg() { let m = ModNum::new(-2, 5); assert_eq!(m, ModNum::new(3, 5)); } #[test] fn test_negation() { for n in 0..5 { let m = ModNum::new(n, 5); let n = -m; assert_eq!(m + n.a(), 0); } } #[test] fn test_sub() { for (n, m, sub, target) in vec![(1, 5, 2, 4)] { assert_eq!(ModNum::new(n, m) - sub, ModNum::new(target, m)); } } #[test] fn test_iter_up() { assert_eq!(vec![2, 3, 4, 0, 1], ModNum::new(2, 5).iter().map(|m: ModNum<usize>| m.a()).collect::<Vec<usize>>()) } #[test] fn test_iter_down() { assert_eq!(vec![1, 0, 4, 3, 2], ModNum::new(2, 5).iter().rev().map(|m: ModNum<usize>| m.a()).collect::<Vec<usize>>()) } #[test] fn test_assign() { let mut m = ModNum::new(2, 5); m += 2; assert_eq!(m, ModNum::new(4, 5)); m += 2; assert_eq!(m, ModNum::new(1, 5)); m -= 3; assert_eq!(m, ModNum::new(3, 5)); m *= 2; assert_eq!(m, ModNum::new(1, 5)); m *= 2; assert_eq!(m, ModNum::new(2, 5)); m *= 2; assert_eq!(m, ModNum::new(4, 5)); m *= 2; assert_eq!(m, ModNum::new(3, 5)); } #[test] fn test_chinese_remainder() { let x = ModNum::new(2, 5); let y = ModNum::new(3, 7); assert_eq!(x.chinese_remainder(y), ModNum::new(17, 35)); } #[test] fn test_chinese_systems() { // Examples from 2020 Advent of Code, Day 13 Puzzle 2. let systems = vec![ (vec![(2, 5), (3, 7), (4, 9)], 157), (vec![(0, 17), (-2, 13), (-3, 19)], 3417), (vec![(0, 67), (-1, 7), (-2, 59), (-3, 61)], 754018), (vec![(0, 67), (-2, 7), (-3, 59), (-4, 61)], 779210), (vec![(0, 67), (-1, 7), (-3, 59), (-4, 61)], 1261476), (vec![(0, 1789), (-1, 37), (-2, 47), (-3, 1889)], 1202161486) ]; for (system, goal) in systems { let mut equations = system.iter().copied() .map(|(a, m)| ModNum::<i128>::new(a, m)); assert_eq!(ModNum::chinese_remainder_system(&mut equations).unwrap().a(), goal); } } #[test] fn test_congruence() { let m = ModNum::new(2, 5); for c in (-13..13).step_by(5) { assert_eq!(m, c); for i in -2..=2 { if i == 0 { assert_eq!(m, c); } else { assert_ne!(m, c + i); } } } } }