You are viewing a single comment's thread. Return to all comments →
Used sieve of eratosthenes and also created another array to keep track of the sums (got the idea from a comment here)
public static void main(String[] args) { final int SIZE = 1000000; boolean[] isPrime = new boolean[SIZE + 1]; Arrays.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]; } } Scanner in = new Scanner(System.in); int t = in.nextInt(); for(int a0 = 0; a0 < t; a0++){ int n = in.nextInt(); System.out.println(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 →
Used sieve of eratosthenes and also created another array to keep track of the sums (got the idea from a comment here)