結果

問題 No.843 Triple Primes
ユーザー htensai
提出日時 2019-12-09 23:56:21
言語 Java
(openjdk 23)
結果
AC  
実行時間 244 ms / 2,000 ms
コード長 989 bytes
コンパイル時間 2,259 ms
コンパイル使用メモリ 75,772 KB
実行使用メモリ 48,332 KB
最終ジャッジ日時 2024-06-23 08:52:44
合計ジャッジ時間 11,920 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 42
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
   public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        TreeSet<Integer> primeSet = new TreeSet<>();
        for (int i = 2; i <= n; i++) {
            if (isPrime(i, primeSet)) {
                primeSet.add(i);
            }
        }
        int count = 0;
        for (int x : primeSet) {
            int y = (int)(Math.sqrt(2 + x));
            if (primeSet.contains(y) && y * y == 2 + x) {
                if (x == 2) {
                    count++;
                } else {
                    count += 2;
                }
            }
        }
        System.out.println(count);
    }
    
    static boolean isPrime(int x, TreeSet<Integer> set) {
        for (int y : set) {
            if (Math.sqrt(x) < y) {
                return true;
            }
            if (x % y == 0) {
                return false;
            }
        }
        return true;
    }
}
0