筛法求素数(java)

import java.util.Scanner;
public class primeShaifa {
	public static void main(String[] args) {
		int n;
		Scanner cin = new Scanner(System.in);
		while (cin.hasNextInt()) {
			n = cin.nextInt();
			int[] array = new int[n];
			for (int i = 2; i < n; i++) {
				array[i] = i;
			}
			for (int i = 2; i < n; i++) {
				if (array[i] != 0) {
					int j, temp;
					temp = array[i];
					for (j = 2 * temp; j < n; j = j + temp) {
						array[j] = 0;
					}
					System.out.print(array[i] + " ");
				}
			}
		}
	}
}

你可能感兴趣的:(【算法与数据结构】)