Created
November 25, 2015 09:09
-
-
Save shoken0x/5367c26e606536393dbb to your computer and use it in GitHub Desktop.
PCC Book: Extended Euclidean algorithm
This file contains 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
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