You are viewing a single comment's thread. Return to all 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]); } }
}
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #10: Summation of primes
You are viewing a single comment's thread. Return to all 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]; } }
}