Skip to content

Instantly share code, notes, and snippets.

@shoken0x
Created November 25, 2015 09:09
Show Gist options
  • Save shoken0x/5367c26e606536393dbb to your computer and use it in GitHub Desktop.
Save shoken0x/5367c26e606536393dbb to your computer and use it in GitHub Desktop.
PCC Book: Extended Euclidean algorithm
def extgcd(a, b)
if b.zero?
{x: 1, y: 0, gcd: a}
else
prev = extgcd(b, a % b)
{ x: prev[:y],
y: prev[:x] - (a / b) * prev[:y],
gcd: prev[:gcd] }
end
end
p extgcd(6728, 4263) # => {:x=>64, :y=>-101, :gcd=>29}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment