結果

問題 No.713 素数の和
ユーザー Pump0129
提出日時 2018-07-17 13:38:22
言語 Java
(openjdk 23)
結果
AC  
実行時間 133 ms / 2,000 ms
コード長 1,173 bytes
コンパイル時間 4,479 ms
コンパイル使用メモリ 76,180 KB
実行使用メモリ 54,328 KB
最終ジャッジ日時 2024-11-24 22:07:00
合計ジャッジ時間 4,544 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 6
権限があれば一括ダウンロードができます

ソースコード

diff #

package net.ipipip0129.yukicoder.no713;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int num = Integer.parseInt(scan.nextLine());
        if (num == 1) {
            System.out.println(0);
            return;
        }
        int sqrt_num = ((int)Math.sqrt(num)) + 1;
        List<Integer> num_list = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            num_list.add(i + 1);
        }
        int pt = 1;
        //エラトステネスの篩
        while (true) {
            int pt_num = num_list.get(pt);
            if (sqrt_num <= pt_num) {
                break;
            }
            for(int i = pt_num * 2; i <= num; i += pt_num) {
                if (num_list.indexOf(i) != -1) {
                    num_list.remove(num_list.indexOf(i));
                }
            }
            pt++;
        }
        int add_num = 0;
        for (int ans_num : num_list) {
            add_num += ans_num;
        }
        System.out.println(add_num - 1);
    }
}
0