Skip to content

Instantly share code, notes, and snippets.

@timjb
Created November 1, 2011 22:49
Show Gist options
  • Select an option

  • Save timjb/1332199 to your computer and use it in GitHub Desktop.

Select an option

Save timjb/1332199 to your computer and use it in GitHub Desktop.
Extended Euclidean algorithm
-- 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