💨

ミラーラビン素数判定法についてメモ

に公開2

Discussion

hamamuhamamu

分かりやすい解説ありがとうございます。
このページのおかげでミラーラビンを理解することができました。
実装で、
a >= n
の時にbreakしていますが、
http://miller-rabin.appspot.com/
のRemarksに、breakしてはいけないと書いてありました。
a ← a mod n
としてテストするそうで、
その際a=0になったらis_primeをtrueとして扱うのが正しいそうです。

Remarks
Let a be primality witness. Let n be the number we test for primality.
Depending on your Miller-Rabin implementation, you may need to take a←a mod n.
When the witness a equals 0, the test should return that n is prime.
It is crucial to test all the bases and not just the bases less than n.

こちらで確認したところ、breakする実装では、
n=4033の時に素数ではないという誤判定になりました(実際は素数)。

hamamuhamamu

もう一点、

求めたい数nが n≤4,759,123,141 ならば
aは 2,7,61 だけ調べればいい

のところ、n=4,759,123,141の時はだめなようです。
nは合成数(48781×97561)ですが、素数に判定されてしまいました。
n<4,759,123,141とするのが正しそうです。