QuestionAsk Questions?

How to show the prime number in Java? Can you please explain it through an example?

View Answers

April 12, 2008 at 7:10 PM

Hi friend,

public class Ex01 {

public static void main(String[] args){

int power_of_two = 2;

for(int n=2; n<31; n++) {

int mersenne = power_of_two - 1;

if (AePrimeExample.checkPrime(mersenne)) {

System.out.print("Found Mersenne prime: ");

System.out.print(mersenne);

System.out.println(" (n = " + String.valueOf(n) + ")");

}

power_of_two *= 2;

}

}

}

public class AePrimeExample {

public static boolean checkPrime(int n){

int primes25[] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,

73,79,83,89,97};

for(int i=0;i<25;i++) {

if (n%primes25[i] == 0) {

if(n == primes25[i]) {

return true;

}

else{

return false;

}

}

}

int maxtest = n/4;

for(int i=259; i<maxtest; i+=2)

if (n%i == 0)

return false;

return true;

}

}

----------------------------

read for more information,

http://www.roseindia.net/java/

public class Ex01 {

public static void main(String[] args){

int power_of_two = 2;

for(int n=2; n<31; n++) {

int mersenne = power_of_two - 1;

if (AePrimeExample.checkPrime(mersenne)) {

System.out.print("Found Mersenne prime: ");

System.out.print(mersenne);

System.out.println(" (n = " + String.valueOf(n) + ")");

}

power_of_two *= 2;

}

}

}

public class AePrimeExample {

public static boolean checkPrime(int n){

int primes25[] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,

73,79,83,89,97};

for(int i=0;i<25;i++) {

if (n%primes25[i] == 0) {

if(n == primes25[i]) {

return true;

}

else{

return false;

}

}

}

int maxtest = n/4;

for(int i=259; i<maxtest; i+=2)

if (n%i == 0)

return false;

return true;

}

}

----------------------------

read for more information,

http://www.roseindia.net/java/

Related Tutorials/Questions & Answers:

Advertisements