Created
November 1, 2011 22:49
-
-
Save timjb/1332199 to your computer and use it in GitHub Desktop.
Extended Euclidean algorithm
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
| -- Extended Euclidean algorithm | |
| -- Preconditions: gcd(a, b) = 1, a > b | |
| -- Postcondition: (fst result) * a + (snd result) * b = 1 | |
| euc :: (Integral a) => a -> a -> (a, a) | |
| euc a b = case b of | |
| 1 -> (0, 1) | |
| _ -> let (e, f) = euc b d | |
| in (f, e - c*f) | |
| where c = a `div` b | |
| d = a `mod` b |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment