the assignment is to find all Mersenne primes with p <= 31 and display in a table:
p 2^p-1
--- ----
2 3
3 7
5 31
...
My result so far is this code:
public class PE28MersennePrimeVer2 {
public static void main(String[] args) {
System.out.println("p\t2^p - 1");
for (int number = 2; number <= 31; number++) {
if (isPrime(number)) {
int mersennePrime = (int)(Math.pow(2, number)) - 1;
if (isPrime(mersennePrime)) {
System.out.print(number + "\t" + mersennePrime + "\n");
}
}
}
}
public static boolean isPrime(int number) {
if ((number == 1) || (number == 2)) {
return true;
}
for (int i = 2; i <= number/2; i++) {
if (number % i == 0) {
return false;
}
}
return true;
}
}
The output is for p up to 19, never reach 31. What I’m doing wrong?
The problem is that
evaluates to 2147483646 instead of 2147483647.
Don’t use floating point math when dealing with integers. In Java, using
(1 << number) - 1works (1 << 31would be undefined behaviour in C due to overflow, but it’s defined in Java).If you cannot use bit-shifts, you can write your own integer-power function. For the small exponents under consideration, the straightforward
is good enough (note: I used
longinstead ofintto avoid overflows; although the overflow behaviour is defined in Java, avoiding overflow is cleaner).With that,
does its job (although you should consider using
longalso for the other variables, not only for the intermediatepowresult).For larger exponents (although that would only be relevant for using
BigIntegers – which have their own implementation in the standard library – or modular exponentiation), exponentiation by repeated squaringwould give a big performance advantage.