Project Euler #10: Summation of primes

  • + 0 comments

    C#

    using System;

    class MainClass { public static void Main (string[] args) { const int SIZE = 1000000; bool[] isPrime = new bool[SIZE + 1]; Array.Fill(isPrime, true); long[] sums = new long[SIZE + 2]; for (int i = 2; i <= SIZE; i++) { if (isPrime[i]) { sums[i] = sums[i-1] + i; for (long j = (long) i * i; j <= SIZE; j+=i) { isPrime[(int)j] = false; } } else { sums[i] = sums[i-1]; } }

        int t = int.Parse(Console.ReadLine());
        for (int a0 = 0; a0 < t; a0++) {
            int n = int.Parse(Console.ReadLine());
            Console.WriteLine(sums[n]);
        }
    }
    

    }