Public Group

# A better way to do it?

This topic is 2779 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I was supposed to make a list of the prime numbers up until 100. Here it is:

#include <iostream>
using namespace std;

int main()
{
double Prime;
int counter;

cout << "List of prime numbers: " << "\n";

Prime = 2;
cout << Prime << "\n";

Prime = 3;
cout << Prime << "\n";

Prime = 5;
cout << Prime << "\n";

Prime = 7;
cout << Prime << "\n";

Prime = 11;
cout << Prime << "\n";

Prime = 13;
cout << Prime << "\n";

Prime = 17;
cout << Prime << "\n";

Prime = 19;
cout << Prime << "\n";

Prime = 23;
cout << Prime << "\n";

Prime = 29;
cout << Prime << "\n";

Prime = 31;
cout << Prime << "\n";

Prime = 37;
cout << Prime << "\n";

Prime = 41;
cout << Prime << "\n";

Prime = 43;
cout << Prime << "\n";

Prime = 47;
cout << Prime << "\n";

Prime = 53;
cout << Prime << "\n";

Prime = 59;
cout << Prime << "\n";

Prime = 61;
cout << Prime << "\n";

Prime = 67;
cout << Prime << "\n";

Prime = 71;
cout << Prime << "\n";

Prime = 73;
cout << Prime << "\n";

Prime = 79;
cout << Prime << "\n";

Prime = 83;
cout << Prime << "\n";

Prime = 89;
cout << Prime << "\n";

Prime = 97;
cout << Prime << "\n";

std::cin.ignore();
std::cin.get();
return 0;
}

At the end it shows what I wanted it to show but... I'm pretty sure that there is a better, shorter way to do this, and I would like to know it.

Thanks.

##### Share on other sites
a) If you want to keep the results hard-coded, output everything at once without using the fairly redundant prime variable and the completely unused integer counter.
b) The proper way: create a function that will find prime numbers for you.
Quote:
 I was supposed to make a list of the prime numbers

You mean it's home work?

##### Share on other sites
For reference, we don't give answers to homework problems here. We can give you hints or point you in the right direction though, or if you ask specific questions and show us your own attempts we can help you out with solving it yourself.

You have a variable called counter, that you're not using... have you learned about loops yet? Notice your program has a series of almost identical lines of code: if this happens, chances are you should be doing something in a loop rather than doing it by hand repeatedly.

Do you know how to figure out if a number is prime or not?

Do those hints help? [wink]

##### Share on other sites
You need to use a loop that checks within the loop if the current number is 100. Example:

for (int i=100;i<=100;i++)
{

//check if "i" is a prime number

}

##### Share on other sites
Hehe, no this is not homework. Well, sort of, I'm studying the C++ book, and the book gives me assignments. And technically, I did it. I was just wondering if there was an easier way to do what I did.

Ok, int counter is a mistake, I wanted to do a loop but I didn't know how to do a loop that would count Prime numbers only, so I scratched that and went the hardcore way.

Now, can anyone please explain, not just tell, an easier way to do this?

##### Share on other sites
Have you covered arrays or containers in your book yet?

##### Share on other sites
Hmm.. I'm not sure. Probably not.

##### Share on other sites
Quote:
 Original post by D4rk_AngelI wanted to do a loop but I didn't know how to do a loop that would count Prime numbers only, so I scratched that and went the hardcore way.
If we picked some random number and asked you whether or not it was prime, would you be able to figure it out? What makes a number prime?

##### Share on other sites
Yeah, it's a number that you cannot factorize, right?

##### Share on other sites
The author of the book probably wants you to make a program that determines which numbers between 1 and 100 is a prime, and print it if it is.

To do this, loops and the modulo operator (%) would come in handy, as well as the square root function (sqrt)

Note in particular the section called "Verifying primality"

quote:
"The most basic method to do this, known as trial division, works as follows: given a number n, one divides n by all numbers m less than or equal to the square root of that number. If any of the divisions come out as an integer, then the original number is not a prime. Otherwise, it is a prime."

• 10
• 18
• 14
• 18
• 15