結果
問題 |
No.713 素数の和
|
ユーザー |
![]() |
提出日時 | 2019-11-12 01:21:51 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 139 ms / 2,000 ms |
コード長 | 629 bytes |
コンパイル時間 | 2,177 ms |
コンパイル使用メモリ | 75,392 KB |
実行使用メモリ | 41,396 KB |
最終ジャッジ日時 | 2024-09-15 12:20:40 |
合計ジャッジ時間 | 4,163 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 6 |
ソースコード
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; } }