• 13
• 18
• 19
• 27
• 10

# Sieve of Sundaram

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

## 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.

include <iostream>#include <vector>int const N = 100; // Generate the primes up to 2*Nint 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';  }}