結果

問題 No.36 素数が嫌い!
ユーザー rn4rurn4ru
提出日時 2016-04-13 04:15:17
言語 Java21
(openjdk 21)
結果
AC  
実行時間 633 ms / 5,000 ms
コード長 1,286 bytes
コンパイル時間 2,225 ms
コンパイル使用メモリ 78,628 KB
実行使用メモリ 60,564 KB
最終ジャッジ日時 2023-09-09 07:36:26
合計ジャッジ時間 9,584 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 119 ms
55,432 KB
testcase_01 AC 201 ms
58,640 KB
testcase_02 AC 118 ms
55,680 KB
testcase_03 AC 117 ms
55,536 KB
testcase_04 AC 119 ms
55,636 KB
testcase_05 AC 153 ms
58,232 KB
testcase_06 AC 164 ms
58,216 KB
testcase_07 AC 153 ms
58,428 KB
testcase_08 AC 118 ms
55,932 KB
testcase_09 AC 119 ms
55,652 KB
testcase_10 AC 118 ms
55,700 KB
testcase_11 AC 370 ms
60,000 KB
testcase_12 AC 633 ms
60,564 KB
testcase_13 AC 627 ms
59,804 KB
testcase_14 AC 181 ms
58,328 KB
testcase_15 AC 118 ms
55,700 KB
testcase_16 AC 117 ms
55,316 KB
testcase_17 AC 117 ms
53,980 KB
testcase_18 AC 118 ms
53,496 KB
testcase_19 AC 118 ms
55,892 KB
testcase_20 AC 217 ms
58,012 KB
testcase_21 AC 118 ms
56,224 KB
testcase_22 AC 119 ms
55,928 KB
testcase_23 AC 117 ms
55,780 KB
testcase_24 AC 294 ms
60,040 KB
testcase_25 AC 119 ms
55,552 KB
testcase_26 AC 377 ms
60,092 KB
testcase_27 AC 203 ms
58,064 KB
testcase_28 AC 379 ms
59,960 KB
testcase_29 AC 190 ms
58,428 KB
権限があれば一括ダウンロードができます

ソースコード

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