problem with prime numbers

Hi,

I'm working on a program to output the number of prime numbers in the first 50,000 numbers, in increments of 1000. Does anyone know what is wrong with this code?

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>

const int CHILIADS = 50;

bool isPrime (long n);
long primeCount (long x, long y);

using namespace std;
int main() 
{	
	cout << setw(10) << left << "Start" << setw(10) << "End" << setw(24) << "Number of Primes" << endl;
	
	primeCount (0, 50000);
	
	return 0;
}

// Determines whether the number is prime
bool isPrime (long n)
{
	int a;
	
	if (n == 1) 
	{
		return false;
	}
	
	for (a = 2; a <= (n / 2); a++) 
	{
		if ((n % a) == 0) 
		{
			return false;
		}
		return true;
	}
}

// Counts and organizes the prime numbers using the isPrime function
long primeCount (long x, long y)
{
	bool prime;
	int b;
	int c = 1000;
	int counter = 0;
	int totalSum = 0;

	for (b = x; b <= y; b++) 
	{
		prime = isPrime (b);
		
		if (prime == true) 
		{
			counter++;
		}
		if (b == c) 
		{
			cout << setw(10) << left << (b - 999) << setw(10) << left << b << setw(12) << counter << endl;
			
			totalSum = totalSum + counter;
			counter = 0;
			c = c + 1000;
		}
	}
	
	cout << endl << "Total primes in the first 50 chiliads: " << totalSum << endl;
	cout << "Average number per chiliad: " << totalSum / CHILIADS << endl;
	
}


The output looks like this:

Start     End       Number of Primes        
1         1000      499         
1001      2000      500         
2001      3000      500         
3001      4000      500         
4001      5000      500         
5001      6000      500         
6001      7000      500         
7001      8000      500         
8001      9000      500         
9001      10000     500         
10001     11000     500         
11001     12000     500         
12001     13000     500         
13001     14000     500         
14001     15000     500         
15001     16000     500         
16001     17000     500         
17001     18000     500         
18001     19000     500         
19001     20000     500         
20001     21000     500         
21001     22000     500         
22001     23000     500         
23001     24000     500         
24001     25000     500         
25001     26000     500         
26001     27000     500         
27001     28000     500         
28001     29000     500         
29001     30000     500         
30001     31000     500         
31001     32000     500         
32001     33000     500         
33001     34000     500         
34001     35000     500         
35001     36000     500         
36001     37000     500         
37001     38000     500         
38001     39000     500         
39001     40000     500         
40001     41000     500         
41001     42000     500         
42001     43000     500         
43001     44000     500         
44001     45000     500         
45001     46000     500         
46001     47000     500         
47001     48000     500         
48001     49000     500         
49001     50000     500         

Total primes in the first 50 chiliads: 24999
Average number per chiliad: 499


But it's supposed to look something like this:

Start   End     Number of Primes
1       1000    168
1001    2000    135
2001    3000    127
3001    4000    120
4001    5000    119
5001    6000    114
6001    7000    117
7001    8000    107
8001    9000    110
9001    10000   112
…
…
…
49001   50000   98

Total primes in the first 50 chiliads: 5133
Average number per chiliad: 102.66


Is it a problem with the prime number calculation, or something else I'm just not seeing? Does anyone know?
Last edited on
While looping through all of the numbers you need to have "return true" on the outside of the for loop for it to work correctly. Or else it will return true after the first check every time.

1
2
3
4
5
6
7
8
9
	for (a = 2; a <= (n / 2); a++) 
	{
		if ((n % a) == 0) 
		{
			return false;
		}
	}

	return true;
Last edited on
Oh! Yeah, now it works. Thanks so much!
Topic archived. No new replies allowed.