//目前自己写出的最好的时间复杂度的算法,应该是o(n).但是自己还不是很确定。
import java.io.*;
public class First
{
static int count = 0;
public static void init(int[] A, int n)
{
int i;
for (i = 0; i < n; i++)
A[i] = i;
}
public static void prime(int[] A, int n)
{
int i, j;
for (i = 2; i < n/2;)
{
for (j = i + i; j < n ; j += i)
A[j] = 0;
i++;
while(A[i] == 0) i++;
}
}
public static void output(int[] A, int n)
{
int i, j = 0;
for (i = 2; i < n; i++)
if (A[i] != 0) {
if (j % 5 == 0)
System.out.print("/n");
System.out.println(A[i]);
j++;
}
}
public static void main(String[] args)
{
int N = 0;
System.out.println("请输入最大素数的范围:");
while (true) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
}
catch (Exception e) {}
if (N < 5 || N > 5000000) {
System.out.println("过大或过小了。重输入:");
continue;
}
else
break;
}
int[] arr = new int[N + 2];
init(arr, N + 2);
long t1 = System.currentTimeMillis();
prime(arr, N + 2);
long t2 = System.currentTimeMillis();
//output(arr, N);
System.out.println("耗时:" + (t2 - t1) + "ms");
System.out.println(arr.length);
}
}