![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jun 2005
Posts: 1
Rep Power: 0
![]() |
i need help in separating composite and prime nos.
just need a basic formula for that. |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#3 |
|
Expert Programmer
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 579
Rep Power: 5
![]() |
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 |
|
|
|
|
|
#4 |
|
Programming Guru
![]() ![]() ![]() |
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." |
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#6 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|