結果

問題 No.713 素数の和
ユーザー htensaihtensai
提出日時 2019-11-12 01:21:51
言語 Java21
(openjdk 21)
結果
AC  
実行時間 130 ms / 2,000 ms
コード長 629 bytes
コンパイル時間 2,096 ms
コンパイル使用メモリ 73,852 KB
実行使用メモリ 56,076 KB
最終ジャッジ日時 2023-10-13 16:20:50
合計ジャッジ時間 4,154 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
56,076 KB
testcase_01 AC 122 ms
55,528 KB
testcase_02 AC 122 ms
55,860 KB
testcase_03 AC 128 ms
55,592 KB
testcase_04 AC 129 ms
55,660 KB
testcase_05 AC 127 ms
55,836 KB
testcase_06 AC 124 ms
55,800 KB
testcase_07 AC 126 ms
55,912 KB
testcase_08 AC 130 ms
55,976 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

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