calculate primes

来源:互联网 发布:js 创建对象数组 编辑:程序博客网 时间:2024/09/21 06:17
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 foundint count = 3 ;// the number of primeslong trial = 5 ; // the candidate prime numberprimes[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);}}