先给出wiki的数据
知道大家比较关心正确,网上教程好难找到数据,我就自己去wiki翻了
if n < 2,047, it is enough to test a = 2;
if n < 1,373,653, it is enough to test a = 2 and 3;
if n < 9,080,191, it is enough to test a = 31 and 73;
if n < 25,326,001, it is enough to test a = 2, 3, and 5
我自己验证了上面四组均通过
如果是uint范围:if n < 4,759,123,141, it is enough to test a = 2, 7, and 61;
如果是10^12范围:if n < 1,122,004,669,633, it is enough to test a = 2, 13, 23, and 1662803;
如果是10^14范围:if n < 341,550,071,728,321, it is enough to test a = 2, 3, 5, 7, 11, 13, and 17.
如果是10^18范围if n < 3,825,123,056,546,413,051, it is enough to test a = 2, 3, 5, 7, 11, 13, 17, 19, and 23.
如果是ull范围:if n < 18,446,744,073,709,551,616 = 2^64, it is enough to test a = 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, and 37.
给出一个MillerRabin的实现方式
我的思路大体是这样的,由费马小定理得 a p − 1 ≡ 1 ( m o d p ) a^{p-1}\equiv1 \pmod p ap−1≡1(modp),如果p是素数先要通过费马小定理的检测,然后再令 k = p − 1 k=p-1 k=p−1,不断除2