clmlbx
Technomancer
Problem:-
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
Now again as my previous post,. Logic is correct it even works for 10,20,2000,20000 but as soon as I enter 2 million it some what crashes.. I thought I need bigger data type so used unsigned long but still not working
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
Code:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int flag = 1;
unsigned long long sum=0,i,j,numb;
cout << "Enter number untill you Want to find sum of primes : " ;
cin >> numb;
for(i=2;i<=numb;i++){
for(j=2;j<=ceil(i/2);j++){
if((i%j) == 0){
flag = 0;
break;
}
}
if(flag == 1){
sum += i;
}else{
flag = 1;
}
}
cout<<endl<<endl<<"Sum of prime Numbers till "<<numb<<" is "<<sum<<endl<<endl;
return 0;
}
Now again as my previous post,. Logic is correct it even works for 10,20,2000,20000 but as soon as I enter 2 million it some what crashes.. I thought I need bigger data type so used unsigned long but still not working