Advertisement
fahimkamal63

Sum of all prime numbers

Apr 17th, 2019
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | None | 0 0
  1. #include<iostream>
  2. #include<cmath>
  3. //#define max_index 1000001
  4. //  The code doesn't run for above value of Max_index
  5. //  But is accepted in Geeks For Geeks
  6. #define max_index 100000
  7. //  But Runs fine for this value
  8. using namespace std;
  9.  
  10. int main(){
  11.     long long int prime[max_index], i, j;
  12.     prime[0] = 0;
  13.     for(i = 1; i < max_index; i++){
  14.         prime[i] = i;
  15.     }
  16.     for(i = 2; i < max_index; i++){
  17.         for(j = i + i; j < max_index; j += i){
  18.             if(prime[j] % i == 0) prime[j] = 0;
  19.         }
  20.     }
  21.     //  Showing the list of Prime numbers
  22.     /*
  23.     for(i = 1; i < max_index; i++){
  24.         if(prime[i] != 0) cout << prime[i] << ' ';
  25.     }
  26.     cout << endl;
  27.     */
  28.  
  29.     int t; cin >> t;
  30.     while(t--){
  31.         int n; cin >> n;
  32.         int result = 0, k = 2;
  33.         while(prime[k] <= n){
  34.             result += prime[k++];
  35.         }
  36.         cout << result << endl;
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement