結果

問題 No.713 素数の和
ユーザー Pump0129Pump0129
提出日時 2018-07-17 13:38:22
言語 Java19
(openjdk 21)
結果
AC  
実行時間 143 ms / 2,000 ms
コード長 1,173 bytes
コンパイル時間 3,316 ms
コンパイル使用メモリ 73,776 KB
実行使用メモリ 56,032 KB
最終ジャッジ日時 2023-08-16 09:54:33
合計ジャッジ時間 6,206 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 143 ms
55,492 KB
testcase_01 AC 119 ms
55,672 KB
testcase_02 AC 119 ms
55,780 KB
testcase_03 AC 134 ms
55,692 KB
testcase_04 AC 143 ms
55,908 KB
testcase_05 AC 129 ms
55,600 KB
testcase_06 AC 119 ms
55,740 KB
testcase_07 AC 120 ms
55,784 KB
testcase_08 AC 132 ms
56,032 KB
権限があれば一括ダウンロードができます

ソースコード

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