Skip to content

Instantly share code, notes, and snippets.

@lovasoa
Created November 8, 2016 16:17
Show Gist options
  • Save lovasoa/0e52bcbc937f3d26224f303669ca2b0f to your computer and use it in GitHub Desktop.
Save lovasoa/0e52bcbc937f3d26224f303669ca2b0f to your computer and use it in GitHub Desktop.
Modular multiplicative inverse in Haskell
-- Inverses a modulo b
inverse b a =
let
next a b = zipWith (-) a $ map (*(head $ zipWith div a b)) b
l = [a,0] : [b,1] : zipWith next l (tail l)
in
head $ tail $ head $ filter ((==1).head) l
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment