java se 求質數 最容易理解的算法

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int flag = 0, temp = 0, count = 1;
for (int a = 1; a <= 100; a++) {
if (a == 2) System.out.printf("2 ");
if (a > 2) {

    for (int i = 2; i < a; i++) {
      if (a % i == 0) {
        flag = 1;
      } else {
        temp = i;
      }
    }
    if (flag == 1) {
      System.out.printf("");
      flag = 0;
    } else {
      count++;
      System.out.printf("%d ", temp + 1);
      if (count == 10) {
        System.out.println();
        count = 0;
      }
    }
  }
}

}
}

發佈了19 篇原創文章 · 獲贊 3 · 訪問量 2197
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章