LoginSignup
0
1

More than 5 years have passed since last update.

Javaで素数を計算

Posted at

何の工夫もないシンプルな方法ですが、1時間ほど回すだけで7978889という素数が得られるなど、なかなか面白いです。

package sosuu;

public class ListSosuuMain {

    public static void main(String[] args) {
        for (int n = 0; n < Integer.MAX_VALUE; n++) {
            if (isSosuu(n) == true) {
                System.err.println(n);
            }
        }
    }

    public static boolean isSosuu(int n) {
        for (int m = 2; m < n; m++) {
            if (n % m == 0) {
                return false;
            }
        }
        return true;
    }
}

0
1
9

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1