Prime(素数) Java

public class Prime{

    public static void printPrime(int n){
        boolean [] prime = new boolean[n+1];

        for(int i=2; i<=n; i++){
            prime[i] = true;
        }

        for(int divisor=2; divisor*divisor<=n; divisor++){
            if(prime[divisor]){
                for(int i=2*divisor; i<=n; i+=divisor)
                {
                    prime[i]=false;
                }
            }
        }

        for(int i=2; i<=n; i++)
        {
            if(prime[i]){
                System.out.print(i+" ");
            }
        }
    }
    public static void main(String[] args) {
        Prime.printPrime(100);
    }
}
0

Leave a Reply

Your email address will not be published.