Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 6th, 2005, 6:06 AM   #1
saadmir
Newbie
 
Join Date: Jun 2005
Posts: 1
Rep Power: 0 saadmir is on a distinguished road
Question Generating prime and composite numbers

i need help in separating composite and prime nos. just need a basic formula for that.
saadmir is offline   Reply With Quote
Old Sep 6th, 2005, 6:25 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
This is a frequent class assignment, solutions abound. Have you Googled?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Sep 8th, 2005, 9:36 PM   #3
Benoit
Expert Programmer
 
Benoit's Avatar
 
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 556
Rep Power: 5 Benoit is on a distinguished road
To find prime numnbers, use the modulous operator
__________________
Johnny was a chemist's son but Johnny is no more, for what Johnny thought was H2O was H2SO4
Benoit is offline   Reply With Quote
Old Sep 9th, 2005, 8:46 AM   #4
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
so us some code, what errors are you getting?
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Sep 9th, 2005, 4:06 PM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Wouldn't want to do your homework for you, so this is a mixture of C and C++ and uses bitfields to boot. It's a Sieve of Eratosthenes. The principles remain the same regardless of your implementation. You will see the modulus operator at work here.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>

using namespace std;

const int N = 16000; // Given that this particular machine has 32-bit storage
                     // elements and we want to process 1000 elements worth of bits.
                     // Changed to 16000 from 32000 because I lose the first 14
                     // lines of output and I'm not about to chase it down!
const int bitsPerInteger = 32;
const int nSetsOf32Ints = N / bitsPerInteger;
/*
Functions for dealing with bits and individual entities
*/
int isSet (int bitNumber);  
void clearBit (int bitNumber);

struct sieve_of_Eratosthenes 
{
   unsigned
   b0:1, b1:1, b2:1, b3:1, b4:1, b5:1, b6:1, b7:1, b8:1, b9:1, b10:1, b11:1,
   b12:1, b13:1, b14:1, b15:1, b16:1, b17:1, b18:1, b19:1, b20:1, b21:1,
   b22:1, b23:1, b24:1, b25:1, b26:1, b27:1, b28:1, b29:1, b30:1, b31:1;

} sieve [nSetsOf32Ints];

int main (void)
{
    int i, j, printoutCount = 0;
    int *mess;

    /* 
    Initialize that sucker by brute force
    */
    for (i = 0; i < nSetsOf32Ints; i++)
    {
        mess = (int *) &sieve [i];
        *mess = 0xffffffff;
    }
    cout << 2 << ", ";
    printoutCount++;
    for (i = 3; i < N; i++)
    {
        if (i % 2) // Ignore all even number markers
        {
            if (isSet (i))  // Lowest 'marked' location.
            {
                cout << i << ", ";
                if (printoutCount++ == 10)
                {
                    cout << endl;
                    printoutCount = 0;
                }
                j = i;
                while (j <= (N - i))    // Subsequent locations to be processed
                {
                      j += i;           // Multiples of what 'i' is right now
                      clearBit (j);
                }
            }
        }
    }
    cout << endl;
    system ("pause");
    return 0;
}
void clearBit (int bit2clear)
{
    int targetInteger;
    int targetBit;
    int clearMask;
    int *mess;
    
    targetInteger = bit2clear / bitsPerInteger;
    targetBit = bit2clear % bitsPerInteger;
    clearMask = 1;
    clearMask <<= targetBit - 1;
    mess = (int *) &sieve [targetInteger];
    *mess &= ~clearMask;
}
int isSet (int bit2test)
{
    int targetInteger;
    int targetBit;
    int peepMask;
    int *mess;

    targetInteger = bit2test / bitsPerInteger;
    targetBit = bit2test % bitsPerInteger;
    peepMask = 1;
    peepMask <<= targetBit - 1;
    mess = (int *) &sieve [targetInteger];
    if (*mess & peepMask) return true; else return false;
}
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Sep 9th, 2005, 4:08 PM   #6
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
http://www.mkaz.com/math/primes.html

use google.
Polyphemus_ is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:48 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC