0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Javaで素数を計算

0
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
8

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?