public class Primes {
public static void cal(int max){
if (max<=4){
max =4;
}
long primes[] = new long[max];
boolean found = true ;// indicate when the prime number found
int count = 3 ;// the number of primes
long trial = 5 ; // the candidate prime number
primes[0] = 2L;
primes[1] = 3L;
primes[2] = 5L;
do{
trial+=2L;
found = false;
for(int i=0;i<count;i++){
found = (trial%primes[i] == 0);
if(found){
break;
}
}
if (!found){
primes[count++] = trial;
}
}while(count<max);
for(int i=0;i<primes.length;i++){
if(i%5==0)
System.out.println(System.getProperty("line.separator"));
System.out.print(" "+primes[i]);
}
}
/**
* @param args
*/
public static void main(String[] args) {
Primes.cal(50);
}
}
calculate primes
最新推荐文章于 2024-07-19 01:44:38 发布