I am currently working on a project for my C++ course and it requires that i find the first 500 prime numbers using an array. How would I go about doing this?
Printable View
I am currently working on a project for my C++ course and it requires that i find the first 500 prime numbers using an array. How would I go about doing this?
Please use more descriptive subject titles.
And nobody here is going to do your homework for you. You will get help if you try it on your own first and then come to us with a specific problem, but you clearly haven't done that yet. Either try writing it on your own, or search the board for some tips.
Welcome to the boards. If you haven't already done so then please take some time to familiarise yourself with the faq:
http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
Make sure you have a look at the the posting guidelines:
http://cboard.cprogramming.com/annou...ouncementid=51
Following the rules will ensure you get a prompt answer to your question.
Remember, too, that the board has a search facility, a link is at the top of your screen:
http://cboard.cprogramming.com/search.php
It will often get you a quicker answer to your questions than waiting for a response to one you have posted.
If you have any questions about this you may ask or you can contact one of our forum leaders:
http://cboard.cprogramming.com/showgroups.php
----------
with that aside, here's your answer:
- find out how to figure out a prime number
- figure out how to use arrays
- figure out how to find a prime number using arrays.
I apologize if by my post it seemed as if I had not attempted the problem. So far I have been working on it for about 3 days now. I understand that to find a prime number I need to divide each individual test value by all of the previous numbers in the array, but for some reason I can't come up with a logical code that will accomplish that. i think that if someone could just push me in the right direction that would be a big help.
Post the code that you have so far. If you haven't even started, start out with a simple loop that will do what you said above.
As of now this is what I have written in:
I'm not quite sure what to put in the parameters for the for and while loops though...Code:#include <iostream>
using namespace std;
int main()
{
int prime[500];
int test = 3;
int i = 1;
cout << "\nThese are the first 500 prime numbers:\n";
prime[0] = 2;
for ()
{
while()
{
if ((test%prime[i]) = 0)
test = test + 2;
else
prime[i + 1] = test;
}
}
Well you do want to have 2 (maybe more- depending on how you do each one) loops. The first is just going to loop through the code to find prime numbers until you have 500. The second loop is going to test each prime number by dividing it by each number below it.
I haven't looked at it that thoroughly, but it looks like you've done a pretty decent job on the logic inside you loops. Keep the purpose of each loop in mind, and that should help you decide how you should set them up.
thanks for your help and I'll be sure that my future posts are a little more thought out before I put them up. I'll just keep plugging away at it.
Sounds good. And now that we've seen that you have actually put work into this (we get a lot of requests from people expecting us to do everything), I will remind of you what I suggested earlier: search the boards. This is a very common assignment, and you might get some ideas from seeing how other people have handled it.
Just wanted to post to let you know that i did figure that problem out. I just kinda tinkered with it (got a TON of errors) but ended up with this as my result:
Thanks for your help in pushing me in the right direction, I'll be sure to come back around for questions and to possibly help out anyone else, if I can.Code:for (j = 0; j <499; j++)
{
for (i = 0; i <= j; i++)
{
if ((test%prime[i]) == 0)
{
test = test + 2;
}
}
prime[i] = test;
test = test + 2;
}
Congratulations - I'm glad you got it working.