Part 3: A prime number is called a Mersenne prime if it can be written in the fo
ID: 3628983 • Letter: P
Question
Part 3: A prime number is called a Mersenne prime if it can be written in the form of 2p – 1 (2 raised tothe p power – 1) for some positive integer p. Write a program that lists all Mersenne primes with p <=
31.
Note that an int will hold values between -231 and +231 – 1, so an int variable can be used for this
problem.
You do not have to use methods in this problem, but you will likely find it easier to use stepwise
refinement techniques described in section 5.12 of the text to simplify the problem.
Explanation / Answer
please rate - thanks
import java.util.*;
public class Mersenne
{public static void main(String[] args)
{int p, n,i, factors;
for(p=1;p<=31;p++)
{n=(int)Math.pow(2,p)-1;
factors=0;
for(i=1;i<=n;i++)
if(n%i==0)
factors++;
if(factors==2)
System.out.print(n+" ");
}
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.