Prime numbers chart

Hey guys. I read the forum rules so I'm just going to be honest, this is a homework question. I'm not looking for a completed program or anything. I'm well ahead of my deadline but I'm not really sure where to go from here or how to structure this. I've been digging through the forum for a couple days now and it helped me figure out how to find the primes but I'm not really sure how to get them into the chart efficiently. The final output is supposed to look like the following. (Broken up by 1,000's and 100's with averages and totals.) The code I have is below.


g1 g2 g3 g4 g5 g6 g7 g8 g9 g10 Average Primes
----------------------------------------------------------------
25 21 16 16 17 14 16 14 15 14 16.8 for 1 to 1000
16 12 15 11 17 12 15 12 12 13 13.5 for 1001 to 2000

8 11 8 8 7 9 8 10 10 8 8.7 for 99001 to 100000

Total number of primes from 1 to 100000: 9592
Average primes per 1000: 95.92
Percentage of primes from 1 to 100000: 9.59




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
//Libraries
#include<iostream>
#include<iomanip>
using namespace std;

//Prototypes
bool isPrime (long);


int main()
{
	int x = 0, y = 0; 
	int temp = 0;

	for (int n = 2; n <= 100000; n++)
	{
		x = isPrime(n);
		temp = x;
			
		if (temp > 0)
			y = y + 1; 
	}

cout << y << endl;

system("PAUSE");
return 0;
}

bool isPrime (long n)
{
	for(int i = 2; i < n ;)
	{		
		if (n > 2)
		{
			if (n % i == 0)
				return false;
			else
				i++;
		}
	}
}



Thanks in advance to anyone who takes the time to help. Appreciate it.
I'm not sure that I understand the chart. These two lines make sense:
Total number of primes from 1 to 100000: 9592
Average primes per 1000: 95.92
but I don't get the next line:
Percentage of primes from 1 to 100000: 9.59
and the rest of the chart, what do the headings mean?
g1 g2 g3 g4 g5 g6 g7 g8 g9 g10 Average Primes
and what are these numbers?
25 21 16 16 17 14 16 14 15 14 16.8 for 1 to 1000

As for the problem as a whole, I'd say you could break it down into two separate concepts, first some code which generates a list of primes. Just think of that as a list of numbers.

Secondly, a part of the program which takes a list of numbers and analyses them. I guess you could think of this as being much the same as if you had a file containing a list of numbers, and you need to analyse them. But I don't understand this part - maybe I'm missing something?

As you seem ready to learn and actually read the forum rules here's a little code review:

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
#include<iostream>
#include<iomanip>


//using namespace std; // Generally considered bad practice, google it for more info

//Prototypes
bool isPrime (const long);

int main()
{
	int x = 0, totalPrimes = 0; //Try to give variables reasonable names
	//int temp = 0; // purpose of temp? dosen't seem to do anything

	for (int n = 2; n <= 100000; ++n)//Prefer ++n over n++ unless you want to retrieve the original value
	{
		x = isPrime(n);
		//temp = x;
			
		if (x > 0)
			totalPrimes = totalPrimes + 1; 
	}

	std::cout << totalPrimes << endl;
	
	// Use this instead of system("PAUSE");
	// visit the begginers forum and go to "Console Closing Down" for a discussion on why
	std::cout << "Press ENTER to continue...";
	std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
	
	return 0;
}

bool isPrime (const long n)// changed n to const as it doesn't change in function
{
	if (n > 3)// moved check outside of loop, the expression will stay the same as n doesn't change, also 3 is prime so you don't need to check against that
	{
		int halfN = (n / 2) + 1; // Only need to check i up to half of n + 1, think about it for a moment and ask why if you can't figure it out
		for(int i = 2; i < halfN ; ++i)
		{		
			if (n % i == 0)
				return false;
		}
	}
	// will only reach here if n <= 2 OR if n is prime
	// that being the case we return true
	return true;
}


As for your question sorry I'm not sure what all those numbers are meant to be.

Edit: Note I didn't actually try to compile this so sorry for any mistakes. (like the one I secretly just fixed)
Last edited on
1
2
std::cout << "Press ENTER to continue...";
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

Actually it is not a good solution either, because it depends on std::cin state. Try to do this after std::cin >> /*some variable*/ for example.
You should be careful using this one.
I think I figured out the numbers.
25 21 16 16 17 14 16 14 15 14
25 is number of primes from 1 to 100, 21 is number from 101 to 200 etc.
Serious problem with your isPrime function. It must return a value. Ensure that it does. And perhaps pay attention to compiler warnings, because it is very likely to be warning you about this.
Chervil yeah sorry about that. The 'g' columns were primes per 100. And then an average at the end.

LBEaston I think you saying I didn't need the "if" statement inside the loop made something in my dumb brain click and it all made sense after that. You'd figure that's a pretty obvious thing I don't know why I didn't see it. Either way that helped a lot. I really appreciate it. And yeah, my teacher has mentioned that namespace standard is bad form, I'm just not comfortable with the std; syntax yet. Next program I'll start though.

Cire yes. That makes a lot of sense. I will most certainly remember that from now on.

Anyway, here's the finished program, if anyone has any critiques I'd be open to hear them. Otherwise thanks again folks!

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Sources: http://primeCountprimeCountprimeCount.cplusplus.com/reference/iomanip/setprimeCount/
// Sources: http://primeCountprimeCountprimeCount.cplusplus.com/forum/beginner/3492/
// Sources: http://cplusplus.shinigami.lt/2012/04/05/prime-number-function/
// Sources: http://primeCountprimeCountprimeCount.cplusplus.com/forum/beginner/102828/ 
// Sources: http://www.cplusplus.com/reference/iomanip/setprecision/
// Sources: http://www.daniweb.com/software-development/cpp/threads/111876/how-to-deactivate-setprecision


//Libraries
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;

//Prototypes
bool isPrime (long);

int main()
{
	int n = 0; 
	double total_average_prime = 0, sub_average_prime = 0;
	double total_prime = 0, sub_prime_01 = 0;

	cout << setw(4) << "g1" << setw(4) << "g2" << setw(4) << "g3" << setw(4) << "g4" << setw(4) << "g5" 
		 << setw(4) << "g6" << setw(4) << "g7" << setw(4) << "g8" << setw(4) << "g9" << setw(5) << "g10" 
		 << "  Average Primes" << endl;
	cout << "------------------------------------------------------" << endl;

	for (int primeCount = 0; primeCount < 100000;)
	{
		for (int x = 0; x < 10; x++)
		{
			for (int y = 0; y < 10; y++)
			{
				for (int z = 0; z < 100; z++)
				{
					n = isPrime(primeCount);
			
					if (n > 0)
					{
					total_prime = total_prime + 1; 
					sub_prime_01 = sub_prime_01 + 1; 
					sub_average_prime = sub_average_prime + 1;
					total_average_prime = total_average_prime + 1; 
					}	
					primeCount++; 
				}
				cout << setw(4) << sub_prime_01;
				sub_prime_01 = 0; 
			}	
			sub_average_prime = (sub_average_prime / 10);
			cout << setw(7) << fixed << showpoint << setprecision(1) << sub_average_prime 
				 << "  for " << noshowpoint << setprecision(0) << setw(5) << left << (primeCount - 999) 
				 << " to " << right << primeCount;
			sub_average_prime = 0; 
			cout << endl;
		}		
	}
	cout << "\nTotal number of primes from 1 to 100000: " << total_prime << endl;
	total_average_prime = (total_average_prime / 100); 
	cout << "Average primes per 1000: " << setprecision(2) << total_average_prime << endl; 
	total_average_prime = (total_average_prime / 10); 
	cout << "Percent of primes from 1 to 100000: " << total_average_prime << endl; 


cout << "\nPress ENTER to quit...";
cin.get();

return 0;
}

bool isPrime (long n)
{
	int halfN = (n / 2) + 1;

	if (n < 2)
		return false; 
	else if (n == 2)
		return true;
	else if (n > 2)
	{
		for(int i = 2; i < halfN; i++)
		{
			if (n % i == 0)
				return false;
		}
	}
	return true; 
}
Topic archived. No new replies allowed.