Project Euler #10: Summation of primes

  • + 0 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); }

        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;
    }
    

    }