Sha256: baec2e5057f475fa3b06c491ce09a9b42f112b64d134897042f9f24bcac45a71
Contents?: true
Size: 405 Bytes
Versions: 15
Compression:
Stored size: 405 Bytes
Contents
# Copyright (c) 2007, 2008 Pythonic Pty Ltd # http://www.pythonic.com.au/ class Integer # Returns modular multiplicative inverse. # Examples: # 2.inverse(7) # => 4 # 4.inverse(7) # => 2 def inverse(m) u, v = m, self x, y = 0, 1 while v != 0 q, r = u.divmod(v) x, y = y, x - q * y u, v = v, r end if u.abs == 1 x < 0 ? x + m : x end end end
Version data entries
15 entries across 15 versions & 1 rubygems