Jump to content
  • Advertisement
Sign in to follow this  
EmrldDrgn

Sieve of Sundaram

This topic is 3065 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've recently become interested in prime number sieves. The Sieve of Eratosthenes is simple, but I'm having quite a hard time understanding some of the other ones, especially the Sieve of Sundaram. Does anyone have/know of any example code for the algorithm, to help me understand how it works? I'd prefer Java or C++ if possible - all I've found so far has been Python, and I don't speak Python.

Share this post


Link to post
Share on other sites
Advertisement
I read beginning of the Wikipedia page, and it seems pretty straight forward:
include <iostream>
#include <vector>

int const N = 100; // Generate the primes up to 2*N

int main() {
std::vector<bool> l(N,true);
for (int j=1;;++j) {
int k=1+3*j;
if (k>=N)
break;
do {
l[k] = false;
k += 2*j+1;
} while (k<N);
}

for (int i=1; i<N; ++i) {
if (l)
std::cout << (2*i+1) << '\n';
}
}



Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!