I have been searching and searching the forums to a possiable solution for my question but I havent been able to find the right answer. i am brand new to using this website and this is the first time I have ever posted. I am writing a program that will print out all the prime numbers less than 2000. I am required to use an array and nested for loops. It is in C++ and I am currently using microsoft visual studio 2008. I know how to find prime numbers and I know how to use arrays and for loops but i am having extreme difficulty in trying to arrange it all in the correct manner. Can anyone please please help me! I have sent my teacher 5 e-mails and he still hasnt responded and i dont have anyone else to ask for help and im just so confused. I just cant make the array work with the nested for loops. Ill take any help I can get. i am just a beginner so please be gentle! Here is what I have so far:
#include <fstream>
using namespace std;
int main ()
{
ofstream outfile;
ifstream infile;
outfile.open ("myfile.txt")
const int count = 2000;
int primes[count];
for (i = 3;i <= 43; i+=2)
{
for (j = 2*i, j< 2000; j+= i)
primes[i] = 0;
Thats what I have so far. My teacher said I should use those for loops but Im so confused somethings not right and I cant figure out how to do this program.
alright i changed things up a bit but still no success. Now i just get repeated 2's when i try to print the numbers. Heres what I did.
outfile << "This is a list of all the prime numbers less than 2000:" << endl;
int a;
int b;
int c;
int d;
const int count = 2000;
int primes[count];
for ( a = 2; a < 2000; a++)
{
for ( b = 3; b < 2000; b++)
{
for (c = 5; c < 2000; c++)
{
for (d = 7; d < 2000; d++)
{
if ( a % b != 0)
outfile << a << endl;
if ( a % c != 0)
outfile << a << endl;
if ( a % d != 0)
outfile << a << endl;
}
}
}
}
You can look up 'The sieve of Eratosthenes' and work out the the algorithm
basicaly:
1 Create a contiguous list of numbers from two to some integer n.
2 Strike out from the list all multiples of two (4, 6, 8 etc.).
3 The list's next number that has not been struck out is a prime number.
4 Strike out from the list all multiples of the number you identified in the
previous step.
5 Repeat steps 3 and 4 until you reach n.
or if you want to get some code:
http://www.cplusplus.com/forum/beginner/11654/#msg55499