利用筛法求质数

 1 package algorithm;
 2 //转载请注明
 3 public class FilterPrime {
 4     public static void filterPrime(int n) {
 5         boolean[] isPrimes = new boolean[n+1];
 6         for(int i=2;i<=n;i++){
 7             isPrimes[i]=true;
 8         }
 9         isPrimes[2]=true;
10         for(int j=2;j<=n;j++){
11             if(isPrimes[j]==true){
12                 for(int m=2;j*m<=n;m++){
13                     isPrimes[j*m]=false;
14                 }
15             }
16         }
17         for(int k=2;k<=n;k++){
18             if(isPrimes[k]==true){
19                 System.out.println(k+"是素数");
20             }
21         }
22     }
23     
24     public static void main(String[] args) {
25         filterPrime(23);
26     }
27 }
When things go wrong,don't go with them.
原文地址:https://www.cnblogs.com/yqy921/p/5571143.html