|
| 1 | +//! Extended Greatest Common Divisor (https://mathworld.wolfram.com/ExtendedGreatestCommonDivisor.html) |
| 2 | +const std = @import("../std.zig"); |
| 3 | + |
| 4 | +/// Result type of `egcd`. |
| 5 | +pub fn ExtendedCommonDivisor(S: anytype) type { |
| 6 | + if (@typeInfo(S) != .int or @typeInfo(S).int.signedness != .signed) { |
| 7 | + @compileError("`S` must be a signed integer."); |
| 8 | + } |
| 9 | + |
| 10 | + return struct { |
| 11 | + gcd: S, |
| 12 | + bezout_coeff_1: S, |
| 13 | + bezout_coeff_2: S, |
| 14 | + }; |
| 15 | +} |
| 16 | + |
| 17 | +/// Returns the Extended Greatest Common Divisor (EGCD) of two signed integers (`a` and `b`) which are not both zero. |
| 18 | +pub fn egcd(a: anytype, b: anytype) ExtendedCommonDivisor(@TypeOf(a, b)) { |
| 19 | + const S = switch (@TypeOf(a, b)) { |
| 20 | + // convert comptime_int to some sized int type for @ctz |
| 21 | + comptime_int => b: { |
| 22 | + const n = @max(@abs(a), @abs(b)); |
| 23 | + break :b std.math.IntFittingRange(-n, n); |
| 24 | + }, |
| 25 | + else => |T| T, |
| 26 | + }; |
| 27 | + |
| 28 | + if (@typeInfo(S) != .int or @typeInfo(S).int.signedness != .signed) { |
| 29 | + @compileError("`a` and `b` must be signed integers"); |
| 30 | + } |
| 31 | + |
| 32 | + std.debug.assert(a != 0 or b != 0); |
| 33 | + |
| 34 | + var x: S = @intCast(@abs(a)); |
| 35 | + var y: S = @intCast(@abs(b)); |
| 36 | + |
| 37 | + // Mantain a = s * x + t * y. |
| 38 | + var s: S = std.math.sign(a); |
| 39 | + var t: S = 0; |
| 40 | + |
| 41 | + // Mantain b = u * x + v * y. |
| 42 | + var u: S = 0; |
| 43 | + var v: S = std.math.sign(b); |
| 44 | + |
| 45 | + while (x != 0) { |
| 46 | + const q = @divTrunc(y, x); |
| 47 | + const old_x = x; |
| 48 | + const old_s = s; |
| 49 | + const old_t = t; |
| 50 | + x = y - q * x; |
| 51 | + s = u - q * s; |
| 52 | + t = v - q * t; |
| 53 | + y = old_x; |
| 54 | + u = old_s; |
| 55 | + v = old_t; |
| 56 | + } |
| 57 | + |
| 58 | + return .{ .gcd = y, .bezout_coeff_1 = u, .bezout_coeff_2 = v }; |
| 59 | +} |
| 60 | + |
| 61 | +test { |
| 62 | + { |
| 63 | + const a: i32 = 0; |
| 64 | + const b: i32 = 5; |
| 65 | + const s, const t = egcd(a, b); |
| 66 | + const g = std.math.gcd(@abs(a), @abs(b)); |
| 67 | + try std.testing.expect(s * a + t * b == g); |
| 68 | + } |
| 69 | + { |
| 70 | + const a: i32 = 5; |
| 71 | + const b: i32 = 0; |
| 72 | + const s, const t = egcd(a, b); |
| 73 | + const g = std.math.gcd(@as(u32, @intCast(a)), @as(u32, @intCast(b))); |
| 74 | + try std.testing.expect(s * a + t * b == g); |
| 75 | + } |
| 76 | + |
| 77 | + { |
| 78 | + const a: i32 = 21; |
| 79 | + const b: i32 = 15; |
| 80 | + const s, const t = egcd(a, b); |
| 81 | + const g = std.math.gcd(@abs(a), @abs(b)); |
| 82 | + try std.testing.expect(s * a + t * b == g); |
| 83 | + } |
| 84 | + { |
| 85 | + const a: i32 = -21; |
| 86 | + const b: i32 = 15; |
| 87 | + const s, const t = egcd(a, b); |
| 88 | + const g = std.math.gcd(@abs(a), @abs(b)); |
| 89 | + try std.testing.expect(s * a + t * b == g); |
| 90 | + } |
| 91 | + { |
| 92 | + const a = -21; |
| 93 | + const b = 15; |
| 94 | + const s, const t = egcd(a, b); |
| 95 | + const g = std.math.gcd(@abs(a), @abs(b)); |
| 96 | + try std.testing.expect(s * a + t * b == g); |
| 97 | + } |
| 98 | + { |
| 99 | + const a = 927372692193078999176; |
| 100 | + const b = 573147844013817084101; |
| 101 | + const s, const t = egcd(a, b); |
| 102 | + const g = std.math.gcd(@abs(a), @abs(b)); |
| 103 | + try std.testing.expect(s * a + t * b == g); |
| 104 | + } |
| 105 | + { |
| 106 | + const a = 453973694165307953197296969697410619233826; |
| 107 | + const b = 280571172992510140037611932413038677189525; |
| 108 | + const s, const t = egcd(a, b); |
| 109 | + const g = std.math.gcd(@abs(a), @abs(b)); |
| 110 | + try std.testing.expect(s * a + t * b == g); |
| 111 | + } |
| 112 | +} |
0 commit comments