結果

問題 No.36 素数が嫌い!
ユーザー rn4ru
提出日時 2016-04-13 04:15:17
言語 Java
(openjdk 23)
結果
AC  
実行時間 619 ms / 5,000 ms
コード長 1,286 bytes
コンパイル時間 2,860 ms
コンパイル使用メモリ 77,156 KB
実行使用メモリ 47,172 KB
最終ジャッジ日時 2024-06-27 00:44:34
合計ジャッジ時間 9,195 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

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 (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