༨ྨʹ͓͚Δ๏ٯݩ
ඞͣ͠๏ٯݩ͕ଘࡏ͢ΔͱݶΒͳ͍
>>> pow(2, -1, 6)
Traceback (most recent call last):
File "", line 1, in
ValueError: base is not invertible for the given modulus
༩͑ΒΕͨ๏ʹରͯ͠ఈ͕๏ٯݩΛ࣋ͨͳ͍ʢԿނʁʣ
ɻ
20 / 33
Slide 21
Slide 21 text
๏ٯݩΛٻΊͯ
๏ٯݩͷҙຯ
a ʹରͯ͠ɺm Λ๏ͱ͢Δ߹ಉํఔࣜ
ax ” 1 (mod m)
Λղ͘͜ͱʹଞͳΒͳ͍ɻ
21 / 33
Slide 22
Slide 22 text
߹ಉͷఆٛʹཱͪฦΔ
๏ٯݩͷҙຯ
ax ” 1 (mod m) Λมܗ͢Δͱɺ1 ࣍ෆఆํఔࣜ
ax ` my = 1
͕ղ x; y Λ࣋ͭ͜ͱʹଞͳΒͳ͍ɻ
22 / 33
Slide 23
Slide 23 text
߹ಉํఔࣜͷղ
ఆཧ
a; c ʹରͯ͠ɺm Λ๏ͱ͢Δ߹ಉํఔࣜ
ax ” c (mod m)
ɺc ͕ gcd(a; m) ͰׂΓΕΔͱ͖ͷΈɺͪΐ͏Ͳ gcd(a; m)
ݸͷޓ͍ʹ߹ಉͰͳ͍ղΛ࣋ͭɻͨͩ͠ɺgcd(a; m) a ͱ m ͷ
࠷େެͰ͋Δɻ
ূ໌ɺదͳॳͷڭՊॻΛࢀর͍ͯͩ͘͠͞ɻ
23 / 33
༨ྨʹ͓͚Δ๏ٯݩ
๏ٯݩ͕ଘࡏ͠ͳ͍έʔε
>>> import math
>>> math.gcd(2, 6)
2
>>> pow(2, -1, 6)
Traceback (most recent call last):
File "", line 1, in
ValueError: base is not invertible for the given modulus
gcd(2; 6) 6= 1 ͳͷͰɺ6 Λ๏ͱ͢Δ 2 ͷ๏ٯݩଘࡏ͠ͳ͍ɻ
26 / 33
Slide 27
Slide 27 text
༨ྨʹ͓͚Δ๏ٯݩ
ఆཧ
a ʹରͯ͠ɺm Λ๏ͱ͢Δ๏ٯݩ͕ଘࡏ͢ΔͨΊͷඞཁे
݅ gcd(a; m) = 1 Ͱ͋Δɻ
ެࣜυΩϡϝϯτʹʮIf mod is present and exp is negative,
base must be relatively prime to mod.ʯͱ͋Δɻ
27 / 33
Slide 28
Slide 28 text
Euclid ͷޓআ๏
ఆཧ
a; b Λ a – b Ͱ͋Δɺr Λ a Λ b Ͱׂͬͨ༨Γͱ͢Δɻ͜ͷ
ͱ͖ɺ
gcd(a; b) = gcd(b; r)
͕Γཱͭɻ
28 / 33
Slide 29
Slide 29 text
Euclid ͷޓআ๏
Euclid ͷޓআ๏ͱ 1 ࣍ෆఆํఔࣜ
a Λ b Ͱׂͬͨͱ༨ΛͦΕͧΕ q; r ͱ͢Δɻ1 ࣍ෆఆํఔࣜ
ax + by = 1 ʹ a = qb + r Λೖ͢Δͱɺ
(qb + r)x + by = 1
)b(qx + y) + rx = 1
ͱͳΔɻ
ͭ·Γɺax + by = 1 ͔Β bs + rt = 1 ͱ͢Δ͜ͱ͕Ͱ͖Δɻ
x = t; y = s ` qt ͱ͍͏ؔɻ
29 / 33
Slide 30
Slide 30 text
pow ͷத
longobject.c ͷίϝϯτʹ͋Δ࣮ʢҰ෦վมʣ
def invmod(a, m):
x, y = 1, 0
while m:
q, r = divmod(a, m)
a, m = m, r
x, y = y, x - q * y
if a == 1:
return x
raise ValueError("Not invertible")
30 / 33
Slide 31
Slide 31 text
༨ྨʹ͓͚Δ๏ٯݩ
ΞϧΰϦζϜ
a ͱ m ͷ࠷େެΛܭࢉ͢ΔաఔͰ๏ٯݩΛܭࢉ͢Δ͜ͱ͕Ͱ
͖Δɻ
Remark
a ʹରͯ͠ɺm Λ๏ͱ͢Δ๏ٯݩΛܭࢉ͢ΔΞϧΰϦζϜ֦
ு Euclid ͷޓআ๏ͱݺΕΔɻ
31 / 33
Slide 32
Slide 32 text
ԿނՃ͞Εͨͷ͔
bpo-36027 ʹॻ͔Ε͍ͯΔཧ༝
Here is another number theory basic that I’ve needed
every now...
ͷجຊతͳ͔ؔͩΒඞཁͩΑͶʂ
32 / 33