I'm trying to Implement the Lucas–Lehmer primality test.
I've two implementations, one in C++ and other in Java, These are follows:
C++:
int p = 86243;
cpp_int M;
bit_set(M, p);
M = M-1; // M = 2^p - 1;
cpp_int S;
S = 4;
while(p>2) {
S = pow(S, 2);
S -= 2;
S %= M;
p--;
}
Java:
int p = 86243;
BigInteger M = BigInteger.ZERO;
BigInteger Two = BigInteger.valueOf(2L);
M = M.setBit(p);
M = M.subtract(BigInteger.ONE); // M = 2^p - 1;
BigInteger S = BigInteger.valueOf(4L);
while(p>2) {
S = S.pow(2).subtract(Two).mod(M);
p--;
}
The Java code runs much faster than C++ code, for C++ I'm using CodeBlocks and Eclipse for Java.
Any reason for that? Am I missing anything particularly in C++ code? Any suggestions will be appreciated.