We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
public class Solution {
public static void Main(string[] args) {
List primes = new List();
for (long i = 2; i <= 1000000; i++) {
if (IsPrime(i)) primes.Add(i);
}
int t = Convert.ToInt32(Console.ReadLine());
for (int a0 = 0; a0 < t; a0++) {
int n = Convert.ToInt32(Console.ReadLine());
long sum = 0;
foreach (long num in primes) {
if (num > n) break;
sum += num;
}
Console.WriteLine(sum);
}
}
public static bool IsPrime(long number) {
if ((number != 2 && number != 3 && number != 5) && (number % 2 == 0 || number % 3 == 0 || number % 5 == 0)) return false;
for (long i = 2; i * i <= number; i++) {
if (number % i == 0) return false;
}
return true;
}
}
Cookie support is required to access HackerRank
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 →
In C#:
using System; using System.Collections.Generic;
public class Solution { public static void Main(string[] args) { List primes = new List(); for (long i = 2; i <= 1000000; i++) { if (IsPrime(i)) primes.Add(i); }
}