Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,570 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,669 people online right now. Registration is fast and FREE... Join Now!




prime numbers

 
Reply to this topicStart new topic

prime numbers

sanjozen
23 Nov, 2007 - 03:35 AM
Post #1

New D.I.C Head
*

Joined: 17 Nov, 2007
Posts: 1


My Contributions
I'm supposed to use the "Sieve of Eratosthenes" algorithm to generate prime numbers without using modulus in the innermost loop.My code below works perfect but I cannot figure out how NOT to use modulus.
Any hints???

CODE
//
// primeNumGenerator.cpp
//
// Samuel Gathairu
// 11/18/07
// Assignment10
// Program uses the "Sieve of Eratosthenes" algorithm to calculate and
// print all prime numbers less than or equal to 20.
//
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
    char outFilename[30];
    bool bArray[21];
    int i, j;
    int count = 0;
    int numCols = 3;

    cout << "This program will generate all prime numbers up to 20 \n";
    cout << "Enter the name of the output file : ";
    cin  >> outFilename;
    
    for( i = 2; i <= 20; i++ )
    {
        bArray[i] = true;
        for( j = 2; j <= i / 2; j++ )
        {
            if( bArray[i] == true )
            {
                for( j = 2; j <= i / 2; j++ )
                   {                            
                    if( (i % j) == 0 ) //-> NO MODULUS WHATSOEVER!!!!
                    {                        
                    bArray[i] = false;
                    }        
                }
            }
        }
    }
    ofstream fout(outFilename);
    fout << "The prime numbers between 0 and 20 are:\n";
    for( i = 2; i <= 20; i++ )
    {
        if( bArray[i] == true )
        {
            if( (count % numCols) == 0 )
            {
                fout << "\n";  
            }
            fout << setw(6) << i << " ";
            count++;
        }
    }    
    fout << endl;
    return 0;
}


Mod Edit: Please use code tags: code.gif

This post has been edited by NickDMax: 23 Nov, 2007 - 08:18 AM
User is offlineProfile CardPM
+Quote Post

juti
RE: Prime Numbers
23 Nov, 2007 - 04:32 AM
Post #2

New D.I.C Head
*

Joined: 9 Nov, 2007
Posts: 9


My Contributions
Your code is not using the "Sieve of Eratosthenes" algorithm. Using the "Sieve of Eratosthenes" algorithm you do not have to use the modulus. Perhaps you have not understood the "Sieve of Eratosthenes" .
User is offlineProfile CardPM
+Quote Post

colo001
RE: Prime Numbers
23 Nov, 2007 - 04:46 AM
Post #3

New D.I.C Head
*

Joined: 6 Nov, 2007
Posts: 2


My Contributions
QUOTE(sanjozen @ 23 Nov, 2007 - 04:35 AM) *

I'm supposed to use the "Sieve of Eratosthenes" algorithm to generate prime numbers without using modulus in the innermost loop.My code below works perfect but I cannot figure out how NOT to use modulus.
Any hints???

CODE
//
// primeNumGenerator.cpp
//
// Samuel Gathairu
// 11/18/07
// Assignment10
// Program uses the "Sieve of Eratosthenes" algorithm to calculate and
// print all prime numbers less than or equal to 20.
//
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
    char outFilename[30];
    bool bArray[21];
    int i, j;
    int count = 0;
    int numCols = 3;

    cout << "This program will generate all prime numbers up to 20 \n";
    cout << "Enter the name of the output file : ";
    cin  >> outFilename;
    
    for( i = 2; i <= 20; i++ )
    {
        bArray[i] = true;
        for( j = 2; j <= i / 2; j++ )
        {
            if( bArray[i] == true )
            {
                for( j = 2; j <= i / 2; j++ )
                   {                            
                    if( (i % j) == 0 ) //-> NO MODULUS WHATSOEVER!!!!
                    {                        
                    bArray[i] = false;
                    }        
                }
            }
        }
    }
    ofstream fout(outFilename);
    fout << "The prime numbers between 0 and 20 are:\n";
    for( i = 2; i <= 20; i++ )
    {
        if( bArray[i] == true )
        {
            if( (count % numCols) == 0 )
            {
                fout << "\n";  
            }
            fout << setw(6) << i << " ";
            count++;
        }
    }    
    fout << endl;
    return 0;
}




seems like a lot of this sieve of erathosthenes is needed. third time i encountered this thing.

anyway your code on initializing the array to true should be separated from the rest of the sieving out nonprimes.

what you actually did was combine both initializing and sieving out which would SURELY lead to errors. also another point here is the fact that you are halving an int which would skip some elements of your array

to solve this initialize all the elements of your array to 1 separate from the other instructions. then starting from 2, get all multiple of the index and set it to false. to do this:

[code]for(int i=2; i < array_size; i++) {
if (array[i] == true) {
for(int j = i + i; j < array_size; j += i) {
array[j] = false;
}
}
}[code]

that should eliminate the need for the modulo.

what the code does is step through the array starting from index 2 and checking if the corresponding array element is true. if true, it sets another index to next multiple of the first index and adding the value of the first index to the second index while setting elements of the array to false


Mod Edit: Please use code tags: code.gif
User is offlineProfile CardPM
+Quote Post

juti
RE: Prime Numbers
23 Nov, 2007 - 05:05 AM
Post #4

New D.I.C Head
*

Joined: 9 Nov, 2007
Posts: 9


My Contributions
I modified your code.
CODE

//
// primeNumGenerator.cpp
//
// Samuel Gathairu
// 11/18/07
// Assignment10
// Program uses the "Sieve of Eratosthenes" algorithm to calculate and
// print all prime numbers less than or equal to 20.
//
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
char outFilename[30];
bool bArray[21];
int i, j,k;
int count = 0;
int numCols = 3;

cout << "This program will generate all prime numbers up to 20 \n";
cout << "Enter the name of the output file : ";

for (i=2;i<=20;i++)
   bArray[i]=true;
   i=2;
while(i*i<20)
{
             if (bArray[i]==true)
             {
                                 j=i*i;
                                
                                 while(j<=20){                                        
                                         bArray[j]=false;
                                        
                                         j+=i;                                  
                                        
                                  
                                  }
             }
             i++;
}
            
                                
            




cout << "The prime numbers between 0 and 20 are:\n";
for( i = 2; i <= 20; i++ )
{
if( bArray[i] == true )
{
if( (count % numCols) == 0 )
{
cout << "\n";
}
cout << setw(6) << i << " ";
count++;
}
}

return 0;
}



I think, it should work now.

This post has been edited by juti: 23 Nov, 2007 - 05:14 AM
User is offlineProfile CardPM
+Quote Post

Bench
RE: Prime Numbers
23 Nov, 2007 - 05:07 AM
Post #5

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 684



Thanked: 24 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
QUOTE(juti @ 23 Nov, 2007 - 12:32 PM) *

Your code is not using the "Sieve of Eratosthenes" algorithm. Using the "Sieve of Eratosthenes" algorithm you do not have to use the modulus. Perhaps you have not understood the "Sieve of Eratosthenes" .

I agree, it looks nothing like the Sieve of Eratosthenes algorithm.

The OP might wish to consider reading this link in depth for some clues on how to change their program
http://en.wikipedia.org/wiki/Sieve_of_eratosthenes
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Prime Numbers
23 Nov, 2007 - 08:32 AM
Post #6

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,868



Thanked: 53 times
Dream Kudos: 550
My Contributions
The idea of the Sieve is to create an array (of one sort or another). Then fill it with numbers 2 - N. Then starting with 2, add 2 to your list of primes, then count by 2's from 4 to N putting 0's in each array element. So if started with:
2 3 4 5 6 7 8 9 ...

you now have

2 3 0 5 0 7 0 9 ...

now look for the next non-zero element, in this case 3, add it to your list of primes and then begin to count by 3's an zero all of those elements.

2 3 0 5 0 7 0 0 0 11 0 13 0 0 0 17 0 19 0 0 22 etc...

now look for the next non-zero element, add it to your list of primes and then begin to count by it...

the idea is to "drop" all the non-prime numbers by removing all multiples of prime numbers. Each prime (p) you do extends your "reach" to p*p, so 2 means you have found all primes from 2 - 4 giving you the table: 2 3 0, which means that you found a prime number 3... well using 3 gives you a table from 3 - 9, which gets you the primes 5 & 7... 5 gets your up to 25, and 7 up to 49...

The sieve is a very fast way to find primes.
User is online!Profile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 10:21PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month