Project Euler #10: Summation of primes
Question
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
Answer : 142913828922
Hacker Rank Problem
Solution
Find the sum of all the primes below two million.
Answer : 142913828922
Hacker Rank Problem
Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | import java.util.*; public class Solution { public static void main(String[] args) { boolean arr[] = new boolean[10000000+1]; for(int i=2;i<=10000000;i++){ arr[i]=true; } for(int j=2;j*j<=10000000;j++){ if(arr[j]){ for(int k=j*2;k<=10000000;k+=j){ arr[k]=false; } } } long arr2[] = new long[10000000+1]; long sum2=0; for(int f=2;f<=10000000;f++){ if(arr[f]){ sum2+=f; } arr2[f]=sum2; } Scanner in = new Scanner(System.in); int t = in.nextInt(); for(int a0 = 0; a0 < t; a0++){ int n = in.nextInt(); System.out.println(arr2[n]); } } } |
Comments
Post a Comment