c++ - integer constant is too large for ‘long’ type when searching for large primes -


i trying find prime numbers between 1,000,000,000,000 , 1,000,000,100,000. code ok if number of digits 4 8, if digits more or equal 12 getting:

  1. warning: integer constant large ‘long’ type
  2. floating point exception
#include <iostream> #define num 1000000100000 using namespace std;  int main() {   bool prime;   long double sum = 0;   (long long int i=1000000000000; i<=num; i++)   {     prime = true;     for(int j=2; j<=i/2; j++)     {       if(i%j == 0) prime = false;     }      if(prime) sum+=i;   }   cout<<sum<<endl; } 

it seems want add prime numbers in range mentioned you. suggest segmented sieve purpose.

or can plain sieving. take array of size 100001 store whether 100000000000 + i prime or not. (better if can 100001 / 2 bits) store odd number 100000000001 + 2 * i , sieve out odd multiples in range 3 1000001 , add remaining numbers.

but still if want stick method, make few suggestion make run in practical time.

#include <iostream> #define num 1000000100000ll  // make long long using namespace std;  int main() {   bool prime;   long double sum = 0;   (long long int i=1000000000001ll; i<=num; += 2ll)  // make long long, loop through odd numbers numbers not prime except 2   {     prime = true;     for(int j=3; j<=1000001; j += 2) // loop until sqrt(i) only, loop through odd numbers     {       if(i%j == 0) {         prime = false;         break;       }     }      if(prime) {         cout << << endl;         sum+=i;      }   }   cout<<sum<<endl; } 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -