Jump to content
  • Advertisement
Sign in to follow this  

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by D4rk_Angel
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.
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 this post


Link to post
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)

There is more info on primes here

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."

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!