ほぼ雑記的メモ
#!/usr/bin/env ruby def inv(t, n) a = n b = t x = 1 y = 0 while b != 1 shift = 0 while (b & (1 << shift)) == 0 if((x & 1) == 1) x = x + n end x /= 2 shift += 1 end b = b >> shift shift = 0 while (a & (1 << shift)) == 0 if((y & 1) == 1) y = y + n end y /= 2 shift += 1 end a = a >> shift if b > a x = x + y b = b - a else y = x + y a = a - b end end x end n = 3801307799 (1..n).each do |t| s = inv(t, n) puts("#{t} * #{s} = #{t * s % n} (mod #{n})") end
Powered by Red Leaf ( Rev. c78c769f2 ), © Issei Numata, 2007-2021