patterncppModerate
Printing 100 prime numbers
Viewed 0 times
prime100printingnumbers
Problem
This is a program I wrote to print out 100 prime numbers. How can I improve it?
```
// Calculating primes
#include
#include
#include
using namespace std;
int main()
{
const int MAX(100); // Number of primes to be identified
long primes[MAX] = {};
long trial(5); // Hold a candidate prime
int count(0); // Count primes found
bool found(false); // Indicates when a prime is found
vector nonPrimes; //Vector to hold non prime numbers
vector disqualify; //Vector to hold prime numbers that disqualify the non prime numbers as prime numbers.
do
{
trial += 2; // Produce next candidate value
found = false; // Reset indicator - assume it is not prime
for (int i = 0; i < count; i++) // Try division by existing primes
{
found = (trial % primes[i]) == 0; // True if no remainder
if (found) // No remainder means number is not a prime
break;
// Terminate the division loop
}
// The above loop will exit either due to a break or after trying all
// existing primes as a divisor. found was initialized to false. If
// found is false after exiting the loop, a divisor was not found.
if (!found) // We have a new prime: not false = true
primes[count++] = trial; // Save candidate in next array position
} while (count < MAX);
// Main loop has completed - we have found MAX prime numbers.
// Display the prime numbers, presenting five numbers on one line.
cout << "Prime numbers found during the program execution:" << endl;
for (int i = 0; i < MAX; i++)
{
if (i % 5 == 0) // For a new line on first line of output
cout << endl; // and on every fifth line that follows
cout << setw(10) << primes[i]; // Provide space between numbers
}
cout << endl; // All primes displayed - for a n
```
// Calculating primes
#include
#include
#include
using namespace std;
int main()
{
const int MAX(100); // Number of primes to be identified
long primes[MAX] = {};
long trial(5); // Hold a candidate prime
int count(0); // Count primes found
bool found(false); // Indicates when a prime is found
vector nonPrimes; //Vector to hold non prime numbers
vector disqualify; //Vector to hold prime numbers that disqualify the non prime numbers as prime numbers.
do
{
trial += 2; // Produce next candidate value
found = false; // Reset indicator - assume it is not prime
for (int i = 0; i < count; i++) // Try division by existing primes
{
found = (trial % primes[i]) == 0; // True if no remainder
if (found) // No remainder means number is not a prime
break;
// Terminate the division loop
}
// The above loop will exit either due to a break or after trying all
// existing primes as a divisor. found was initialized to false. If
// found is false after exiting the loop, a divisor was not found.
if (!found) // We have a new prime: not false = true
primes[count++] = trial; // Save candidate in next array position
} while (count < MAX);
// Main loop has completed - we have found MAX prime numbers.
// Display the prime numbers, presenting five numbers on one line.
cout << "Prime numbers found during the program execution:" << endl;
for (int i = 0; i < MAX; i++)
{
if (i % 5 == 0) // For a new line on first line of output
cout << endl; // and on every fifth line that follows
cout << setw(10) << primes[i]; // Provide space between numbers
}
cout << endl; // All primes displayed - for a n
Solution
- STOP. Do not use
system("pause"). This is not portable. If you
haven't already, read
this
- Stop using
using namespace std. Read
this.
I mentioned this in your last review as well.
- You don't need to use
std::endlfor every new line of your output
table. Doing so flushes the output stream needlessly.
- You don't need two vectors. Just two
forloops and one vector will
suffice. Take a look at the example below.
- For beginner code, I think you have good documentation(comments).
Although for some people, it might err on the side of excessive.
-
You could have initialized the array with the first three primes.
That would have cut down the number of iterations.
long primes[MAX] = {2,3,5};
int count(0); // Count primes foundExample:
#include
#include
#include
using std::cin;
using std::cout;
using std::endl;
using std::setw;
int main()
{
int counter = 0;
std::vector primes;
int N, output;
cin >> N;
output = N;
for (int i=2; N > 0; ++i)
{
bool isPrime = true;
for (int j=0; j < primes.size(); ++j) //First pass(i==2), will not enter second loop because size() == 0
{
if (i % primes[j] == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
{
primes.push_back(i);
--N;
cout << i << "\n";
}
}
for(int i=0; i < output; i++)
{
if (i % 5 == 0) // For a new line on first line of output
cout << "\n"; // and on every fifth line that follows
cout << setw(10) << primes[i]; // Provide space between numbers
}
cout << endl;
return 0;
}Code Snippets
long primes[MAX] = {2,3,5};
int count(0); // Count primes found#include <iostream>
#include <vector>
#include <iomanip>
using std::cin;
using std::cout;
using std::endl;
using std::setw;
int main()
{
int counter = 0;
std::vector<int> primes;
int N, output;
cin >> N;
output = N;
for (int i=2; N > 0; ++i)
{
bool isPrime = true;
for (int j=0; j < primes.size(); ++j) //First pass(i==2), will not enter second loop because size() == 0
{
if (i % primes[j] == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
{
primes.push_back(i);
--N;
cout << i << "\n";
}
}
for(int i=0; i < output; i++)
{
if (i % 5 == 0) // For a new line on first line of output
cout << "\n"; // and on every fifth line that follows
cout << setw(10) << primes[i]; // Provide space between numbers
}
cout << endl;
return 0;
}Context
StackExchange Code Review Q#66468, answer score: 12
Revisions (0)
No revisions yet.