結果

問題 No.36 素数が嫌い!
コンテスト
ユーザー rn4ru
提出日時 2016-04-13 04:15:17
言語 Java
(openjdk 25.0.2)
コンパイル:
javac -encoding UTF8 _filename_
実行:
java -ea -Xmx700m -Xss256M -DONLINE_JUDGE=true _class_
結果
AC  
実行時間 379 ms / 5,000 ms
コード長 1,286 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,977 ms
コンパイル使用メモリ 83,104 KB
実行使用メモリ 52,108 KB
最終ジャッジ日時 2026-03-14 01:01:09
合計ジャッジ時間 6,084 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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 (isComposite(N)) {
			System.out.println("YES");
		} else {
			System.out.println("NO");
		}

	}

	private static boolean isComposite(BigInteger n) {
		if (n.equals(BigInteger.ONE)) {
			return false;
		}

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

		if (n.mod(two).equals(BigInteger.ZERO)) {
			return !isPrime(n.divide(two));
		}

		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)) {
				return !isPrime(n.divide(i));
			}
		}

		return false;
	}

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

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

		if (n.mod(two).equals(BigInteger.ZERO)) {
			return false;
		}

		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)) {
				return false;
			}
		}
		return true;
	}

}
0