結果

問題 No.713 素数の和
ユーザー mame_shibemame_shibe
提出日時 2019-12-07 20:32:20
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 60 ms / 2,000 ms
コード長 1,493 bytes
コンパイル時間 890 ms
コンパイル使用メモリ 60,996 KB
実行使用メモリ 22,640 KB
最終ジャッジ日時 2023-08-27 01:46:38
合計ジャッジ時間 2,298 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 58 ms
22,624 KB
testcase_01 AC 58 ms
20,612 KB
testcase_02 AC 59 ms
22,636 KB
testcase_03 AC 60 ms
20,584 KB
testcase_04 AC 58 ms
20,696 KB
testcase_05 AC 60 ms
22,532 KB
testcase_06 AC 60 ms
20,712 KB
testcase_07 AC 60 ms
20,720 KB
testcase_08 AC 58 ms
22,640 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

using System;
using System.Text;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using static System.Console;
using static System.Math;

namespace YukiCoder
{
    public class Program
    {
        public static void Main(string[] args)
        {
            new Program().Solve();
        }

        public void Solve()
        {
            int N = int.Parse(Console.ReadLine());
            int n = 1000;
            bool[] primes = SieveOfEratosthenes(n);
            int sum = 0;

            for (int i = 0; i <= N; i++)
            {
                if (primes[i])
                {
                    sum += i;
                }
            }
            
            WriteLine(sum);

        }
        
         public static bool[] SieveOfEratosthenes(int n)
        {
            bool[] isPrime = new bool[n+1];
            for (int i = 0; i < n; i++)
            {
                isPrime[i] = true;
            }
        
            isPrime[0] = false;
            isPrime[1] = false;
        
            for (int i = 2; i < Math.Sqrt(n); i++)
            {
                int j;
                if (isPrime[i])
                {
                    j = i + i;
                    while (j <= n)
                    {
                        isPrime[j] = false;
                        j = j + i;
                    }
                }
            }

            return isPrime;
        }

    }
}
0