Problem:
The Minimite friar Marin Mersenne (1588–1648) undertook in 1644 the study of numbers of
the form n = 2p – 1, where p is a prime. He believed that most of these n are also primes, now called Mersenne primes.Write a program that finds all the Mersenne primes for p < 30. Use
the Primes class from previous posts. Your first five lines of output should look like this:
the form n = 2p – 1, where p is a prime. He believed that most of these n are also primes, now called Mersenne primes.Write a program that finds all the Mersenne primes for p < 30. Use
the Primes class from previous posts. Your first five lines of output should look like this:
Output:
2 2^2-1 = 3 is prime
3 2^3-1 = 7 is prime
5 2^5-1 = 31 is prime
7 2^7-1 = 127 is prime
11 2^11-1 = 2047 is not prime
3 2^3-1 = 7 is prime
5 2^5-1 = 31 is prime
7 2^7-1 = 127 is prime
11 2^11-1 = 2047 is not prime
Solution:
public class TestMersenne {
public static void main(String[] args) {
Primes.setSize(1000);
for (int p = Primes.next(); p < 30; p = Primes.next()) {
int n = (int)Math.round(Math.pow(2,p)) - 1;
System.out.printf("%d\t2^%d-1%d", p, p, n);
if (Primes.isPrime(n)) {
System.out.println(" is prime ");
} else {
System.out.println(" is not prime ");
}
}
}
}
No comments :
Post a Comment