calculate the number of prime numbers between two inputs

ive done the main parts... but i dont get how to calculate the number of prime numbers in between. for instance if the input is 8 and 2, the prime numbers will be 7,5,3,2. i want to output "There are __ prime numbers between"

I believe increment should be used, but where do i put it ?

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <cmath>

using namespace std;

bool isprime(unsigned int x)
{
	unsigned int i = 2;
	unsigned int count = 0;

	while(i<x)
	{
		if(x%i == 0)
			return false;
		else
		{
			i = i+1;
		}
	}

	cout << i << " prime" << endl;

	return 0;
}

int main()
{
	unsigned int m, n;
	int i;
	int count = 0;

	cout << "Please input two positive integers m and n" << endl;
	cin >> m >> n;


	if(m < n)					// m = 2, n = 8
	{
		for(i=m; i<n+1; i++)
		{			
			if(isprime(i))
			{
				count++;
			}
		}
	}

	cout << "There are " << count << " prime numbers between them." << endl;

	system("pause");
	return 0;
}
Last edited on
line 40

should be if (isprime(i))? If your isprime function works, your program will be stuck at an infinite loop at that point

Counter should be on line 42
Last edited on
Thanks for the reply.
I changed it, but the results are the same.

i would get 7, 5, 3, 2 is prime
but "There are 0 prime numbers between them"

:(
Try this

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
37
38
39
40
41
42
#include <iostream>

using namespace std;

bool isprime(int number)
{
    int numberOfFactors = 0;

    for(int divisor=2; divisor<=number/2; divisor++)
    {
            if(number%divisor == 0)
                     numberOfFactors++;
    }

    if(numberOfFactors == 0)
        return false;
    else
        return true;
}

int main()
{
	unsigned int m, n;
	int i;
	int count = 0;

	cout << "Please input two positive integers m and n" << endl;
	cin >> m >> n;

	if(m > n)					// m = 7, n = 3
	{
		for(i=m; i>n-1; i--)
		{
			if(!isprime(i))
                                count++;
		}

		cout << "There are " << count << " prime numbers between them." << endl;
	}

}
Thank You Sooo much!!

I have one more question... what does it mean by "use an array to store the prime numbers you have found?"
Last edited on
Do you know what an array is?
Yes. its a series of elements of the same type placed in contiguous memory locations which can be individually referenced by adding an index to a unique identifier via the use of []?

How would i store the prime numbers found? what does the requirement mean? does it mean to save the prime numbers ? but why would the program want to do that if the input will be changed everytime it runs?
you can save time by re-use the primes saved in the array.

Example:

- User asks for primes in range [2,8]
loop range [2,8] (7 iters)
save {2,3,5,7}
answer: 4

- User asks for primes in range [4,20]
loop range [9,20] (12 iters instead of [4-20] which is 15 iters --> save 3 iters)
answer: 6
save {2,3,5,7,11,13,17,19}

- User asks for primes in range [3,16]
loop range: none 0 iter --> faster than 14 iters
answer: 5
save {2,3,5,7,11,13,17,19}

- User asks for primes in range [1, 10 000 000]
loop range: [21, 100 000] ~ 10mil iters, not saving much time here...
answer: N
save {2,3,5,7,11,13,17,19,...,999983}

- User asks for primes in range [1, 9 000 000]
loop range: none 0 iter --> super fast, saved 9mil iters!
answer: n
save {2,3,5,7,11,13,17,19,...,999983}
Thanks tntxtnt. Any clues on how to store them in arrays ?
Topic archived. No new replies allowed.