
A function for finding the modular multiplicative inverse, based on an extended version of the 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 EGCD(b,m,recLevel=0) | |
if b % m == 0 | |
tmpVal = [0,1] | |
return tmpVal | |
else | |
tmpVal = EGCD(m, b % m, recLevel+1) | |
tmpVal2 = [tmpVal[1], tmpVal[0]-tmpVal[1] * ((b/m).to_i)] | |
if recLevel == 0 | |
return tmpVal2[0] % m | |
else | |
return tmpVal2 | |
end | |
end | |
end |
No comments:
Post a Comment