Write a program that finds all the Mersenne primes for p < 30 in java

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:

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

Solution:

01public class TestMersenne {
02public static void main(String[] args) {
03Primes.setSize(1000);
04for (int p = Primes.next(); p < 30; p = Primes.next()) {
05int n = (int)Math.round(Math.pow(2,p)) - 1;
06System.out.printf("%d\t2^%d-1%d", p, p, n);
07if (Primes.isPrime(n)) {
08System.out.println(" is prime ");
09} else {
10System.out.println(" is not prime ");
11}
12}
13}
14}


No comments :

Post a Comment

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact