結果

問題 No.36 素数が嫌い!
ユーザー rn4ru
提出日時 2016-04-13 03:56:28
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 855 bytes
コンパイル時間 2,329 ms
コンパイル使用メモリ 74,952 KB
実行使用メモリ 58,992 KB
最終ジャッジ日時 2024-10-04 06:55:51
合計ジャッジ時間 12,327 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3 WA * 1
other AC * 23 WA * 3
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.math.BigInteger;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		BigInteger N = new BigInteger(scanner.next());

		if (isPrime(N)) {
			System.out.println("NO");
		} else {
			System.out.println("YES");
		}

	}

	private static boolean isPrime(BigInteger n) {
		if (n.equals(BigInteger.ONE)) {
			return true;
		}

		BigInteger two = new BigInteger("2");
		if (n.equals(two)) {
			return true;
		}

		boolean once = false;
		if (n.mod(two).equals(BigInteger.ZERO)) {
			once = true;
		}

		BigInteger three = new BigInteger("3");
		for (BigInteger i = three; i.multiply(i).compareTo(n) < 1; i = i.add(two)) {
			if (n.mod(i).equals(BigInteger.ZERO)) {
				if (once) {
					return false;
				} else {
					once = true;
				}
			}
		}

		return true;
	}

}
0