In math, a prime number is a number only divisible by 1 and itself.
Given the first few prime numbers2 3 5 7 11 13 17 ...
What is the sum of the first 250 prime numbers? Note that you are seeing the first 7 primes above.
Language C++:
#include<iostream>
using namespace std;
class prime {
private:
unsigned int sum_prime;
public:
prime() {
sum_prime = 2;
}
void sum_of_primes_calc() {
int pcounter = 1;
int num = 3;
int i,counter = 0;
while(pcounter != 250) {
for (i = 2 ; i < num ; i++) {
if((num % i) == 0) {
counter++;
}
}
if(counter == 0) {
sum_prime += num;
pcounter++;
}
counter = 0;
num++;
}
cout<<sum_prime<<endl;
}
};
int main() {
prime obj;
obj.sum_of_primes_calc();
return(0);
}
Output:
182109
Note: To find the sum of primes beyond 250, just change the value in while condition "While(pcounter != 250)"
By +Dixit Singla
No comments:
Post a Comment