Created
November 21, 2021 11:53
-
-
Save thyeem/e0cbdf1b1de8448032c17742737b897e to your computer and use it in GitHub Desktop.
Solve a given system of congruences using Chinese Remainder Theorem (CRT)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- | Solve a given system of congruences using Chinese Remainder Theorem (CRT) | |
-- [(Integer, Integer)] = [(eq.1 residue, eq.1 modulo), (eq.2 residue, eq.2 modulo),..] | |
chineseRemainder :: [(Integer, Integer)] -> Maybe Integer | |
chineseRemainder congruences = | |
(`mod` _N) | |
. sum | |
. hadamard _Ni | |
. hadamard residues | |
<$> zipWithM getMi _Ni moduli | |
where | |
(residues, moduli) = unzip congruences | |
_N = product moduli | |
_Ni = (_N `div`) <$> moduli | |
hadamard = zipWith (*) | |
getMi a p | (a * inv) /% p == 1 = Just inv | |
| otherwise = Nothing | |
where inv = a /% p | |
-- | Extended Euclidean algorithm based on Bezout's identity | |
-- ax + by = gcd(a, b), returns (x, y) | |
egcd :: Integer -> Integer -> (Integer, Integer) | |
egcd _ 0 = (1, 0) | |
egcd a b = (y, x - q * y) | |
where | |
(q, r) = quotRem a b | |
(x, y) = egcd b r | |
-- | Reciprocal in Z/pZ | |
-- By Extended Euclidean algorithm: egcd(a, p) -> (x, y) | |
(/%) :: Integer -> Integer -> Integer | |
a /% p = fst $ egcd a p |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment