Sum of prime numbers function

I need to write a function that gives the sum of prime numbers between 1 and N.

Then a program that uses this function to output the sum of prime numbers between 1 and N

Im just confused as to how to write the function as prime numbers dont increase in fixed intervals? I understand this is probably ridiculously simple but I'm just beginning

Thank you in advance
To find if a number is prime, you need to check if it has any divisors up to square root of n.

e.g. is 21 prime? square root is 4.582

2 doesn't go in it.
3 does go in it - 21 is not prime

is 19 prime? square root is 4.35

2 doesn't go into it
3 doesn't go into it
4 doesn't go into it
we reached root of 19 - 19 is prime

You can get square roots by using sqrt(n) for which you will need to #include <cmath>.

You can optimize this algorithm a great deal, but just get it working to start with.
Thank you so much! Here is a sample for the class:


#include<iostream>
using namespace std;

#include <cmath>
#include <iostream>

using namespace std;

int sum_primes(int N)
{
cout << "Enter a number:\t ";
cin >> N;*/
int i, count = 0, sum = 0, x;
bool prime;

for(x = 2; x <= N; x++) {
prime = true;

for(i = 2; i <= sqrt(x); i++) {

if (x % i == 0) {
prime = false;
count++;
}
}
if (prime) sum += x;

}
if (count == 0 && N>3)
sum += N;

return sum;
}

int main(){
int Q;
cout << "Enter a number:\t ";
cin >> Q;
cout << "The sum of these primes is: " << sum_primes(Q);

return 0;

}

Can someone talk me through this?
I understand it as
Naming the function, asks user to enter a number. I dont understand what 'bool prime' means. Then When x is 2 and less than the chosen number, add 1 to it. When i is less than the squareroot of x, add 1. If i has no remainder then it is not a prime. If it is a prime, add them. return result. Then asks user to enter number, prints the sum of the numbers is: then names function so program uses the method named in the function to get the answer?

Thank you


Here is a version that does not need the math.h header
Simple program that divides each number uptil the nth value by all values till one smaller than the number.

Eg: If nth value is 5, it will take int i and divide it till (i - 1). if remainder is zero then its not a prime.

Note: Program does take care of division by one.

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
32
33
34
35
36
#include <iostream>
using namespace std;

int prime(int n);

void main()
{
	int n;
	cout << "Enter nth Value: ";
	cin >> n;
	cout << "The Sum Is: " << prime(n);
}

int prime(int n)
{
	int num = 0;
	bool prime;
	for(int i = 2; i <= n; i++)
	{
		prime = true;
		for(int j = 2; j < i; j++)
		{
			if(i % j == 0)
			{
				prime = false;
				break;
			}
		}
		if(prime)
		{
			num += i;
		}
	}
	return num;
}


bool prime is just a variable.
the bool data type returns a true or false value.
In the program if the remainder during division is 0, then prime is set to false - that means that the value of 'i' at that instant is not a prime number and so no need to add it to 'num' .

also the if(prime) iterator works as the if statement will execute any value other than 0.
0 - false, any other value - true.
Last edited on
Topic archived. No new replies allowed.