File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed
OOP in Java/Chapter 10 - Object-Oriented Thinking/Code_Example Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change 1+ package ch_10 ;
2+
3+ import java .math .BigInteger ;
4+
5+ /**
6+ * 10.19 (Mersenne prime) A prime number is called a Mersenne prime if it can be written in the form 2^p - 1 for some
7+ * positive integer p. Write a program that finds
8+ * all Mersenne primes with p … 100 and displays the output as shown below.
9+ * (Hint: You have to use BigInteger to store the number, because it is too big to
10+ * be stored in long. Your program may take several hours to run.)
11+ */
12+ public class Exercise10_19 {
13+ static int P = 2 ;
14+ static BigInteger TWO = BigInteger .valueOf (2L );
15+
16+ public static void main (String [] args ) {
17+ System .out .print ("p 2^p - 1" );
18+
19+ while (P <= 100 ) {
20+ BigInteger mersennePrime = TWO .pow (P ).subtract (BigInteger .ONE );
21+ if (mersennePrime .isProbablePrime (1 )) {
22+ System .out .print ("\n " + P + " " + mersennePrime .toString ());
23+ System .out .println ();
24+ P ++;
25+ }
26+ }
27+ }
28+
29+ }
30+
You can’t perform that action at this time.
0 commit comments