在RSA中计算m的e次方mod n 有一个快速算法 如下例 用a^b表示a的b次方
3^22 mod 12
= 9^11 mod 12
= 9×(9^10) mod 12
= 9×(9^5) mod 12
= (9^2)×(9^4) mod 12
= 9×(9^2) mod 12
= 9×9 mod 12
= 9
这里9的2a次方mod12 为什么会等于9的a次方mod12呢?用的什么定理?
3^22 mod 12
= 9^11 mod 12
= 9×(9^10) mod 12
= 9×(9^5) mod 12
= (9^2)×(9^4) mod 12
= 9×(9^2) mod 12
= 9×9 mod 12
= 9
这里9的2a次方mod12 为什么会等于9的a次方mod12呢?用的什么定理?
3^2a=(3^2)^a=9^a
(9*8 + 9)^a除了最后一项是9^a,其它的项都有9*8,模12的结果就是9^a%12
3^22 mod 12
= 9^11 mod 12 1L正解
an mod n = a
for any integer a and any prime n. This means that if n is prime then an mod n = a for any integer a. The contrapositive of this statement says that if an mod n != a for some integer a then n is NOT prime.
Unfortunately, the converse of the theorem is not true. This means that if an mod n = a, we cannot be sure whether n is prime. However, we can make use of the contrapositive to determine if n is composite. In other words, if there exists an integer a such that an mod n != a, then n is not prime. By testing out sufficient numbers of a's, we can exclude numbers that are composite and retain numbers that are probably prime. The following source code illustrates this.
// confidence determines the number of times to test.for(int round = 0; round < confidence; round++)
{
// generate random a < n
a.genRandomBits(testBits, rand); // calculate a^(n) mod n, where thisVal = n
BigInteger expResult = a.modPow(thisVal, thisVal); // is NOT prime if a^(p) mod p != a
if(expResult != a)
return false;
}return true;
There is a class of composite integer known as Carmichael number that cannot be differentiated from real primes using this test. Such integers satisfy an mod n = a for all positive integers a with gcd(a,n) = 1.