import java.util.
*
;
public class Prime
... {
public static ArrayList findPrimes(final int max)
...{
int[] prime = new int[max+1];
ArrayList list = new ArrayList();
for(int i = 2; i <= max; i++)
prime[i] = 1;
for(int i = 2; i*i <= max; i++)
...{
if(prime[i] == 1)
...{
for(int j = 2*i; j <= max; j++) //Eratosthenes筛选求质数(2的倍数)
...{
if(j % i == 0)
prime[j] = 0;
}
}
}
for(int i = 2; i < max; i++)
...{
if(prime[i] == 1)
...{
list.add(new Integer(i));
}
}
return list;
}
public static void main(String[] args)
...{
ArrayList primelist = findPrimes(100);
for(int i = 0; i < primelist.size(); i++)
...{
System.out.printf("%3d ",primelist.get(i));
}
System.out.println();
}
}
public class Prime
... {
public static ArrayList findPrimes(final int max)
...{
int[] prime = new int[max+1];
ArrayList list = new ArrayList();
for(int i = 2; i <= max; i++)
prime[i] = 1;
for(int i = 2; i*i <= max; i++)
...{
if(prime[i] == 1)
...{
for(int j = 2*i; j <= max; j++) //Eratosthenes筛选求质数(2的倍数)
...{
if(j % i == 0)
prime[j] = 0;
}
}
}
for(int i = 2; i < max; i++)
...{
if(prime[i] == 1)
...{
list.add(new Integer(i));
}
}
return list;
}
public static void main(String[] args)
...{
ArrayList primelist = findPrimes(100);
for(int i = 0; i < primelist.size(); i++)
...{
System.out.printf("%3d ",primelist.get(i));
}
System.out.println();
}
}
有关的定理:
[定理1]若比素数P小的所有素数的倍数均已从sieve中删去,且P*P > N,则sieve = primes(2…N)。 其中:primes(2..N)表示2..N之间所有的素数。
[定理2] 已知素数P,在P和P*P之间至少存在一个素数。