bitsets and primes (C++)

Programming, for all ages and all languages.
Post Reply
User avatar
Zacariaz
Member
Member
Posts: 1069
Joined: Tue May 22, 2007 2:36 pm
Contact:

bitsets and primes (C++)

Post by Zacariaz »

I have taken a different aproach to the prime sieve and it seems to work, however i got this feeling that there is something wrong, though i fail to see what.

I would very much appresiated if someone could look at the code, just to make sure.

Code: Select all

#include <bitset>
class Prime {
        std::bitset<0x10000> Pb;
    public:
            // initializes PB.
        Prime():Pb(3) {
            // with all bits set exept 0, 1.
            Pb.flip();
            // outer loop.
            for(long n = 0; n < 0xff; n++) {
                // find the next set bit (pos n).
                for(; Pb[n] != 1; n++) {}
                // resets all bit which n divides, exept n.
                for(long i = n+n; i <= 0xffff; Pb.reset(i), i += n) {}
            }
        }
};
All this does is initializing a bitset with a "prime pattern", i really dont know what else to call it.
Next step is to create a simple function which takes an unsigned long/int as an argument and outputs a bool value, 1 if prime, 0 if not.
I have trouble figuring out the right aproach for this and idea would be appreciated.

Generel advise and stuff are of course allso welcome.


Thanks
This was supposed to be a cool signature...
User avatar
Zacariaz
Member
Member
Posts: 1069
Joined: Tue May 22, 2007 2:36 pm
Contact:

Post by Zacariaz »

did i put this in the wrong forum by accident or did someone move it?

I guess it belongs here anyway.
This was supposed to be a cool signature...
Post Reply