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