Skip to content

Instantly share code, notes, and snippets.

@kimwalisch
Last active March 4, 2018 18:47
Show Gist options
  • Select an option

  • Save kimwalisch/28b1308ecbf33260bb2e243fab3459b0 to your computer and use it in GitHub Desktop.

Select an option

Save kimwalisch/28b1308ecbf33260bb2e243fab3459b0 to your computer and use it in GitHub Desktop.
Set the segment size in the segmented sieve of Eratosthenes
/// Set your CPU's L1 data cache size (in bytes) here
const int64_t L1D_CACHE_SIZE = 32768;
/// Generate primes using the segmented sieve of Eratosthenes.
/// This algorithm uses O(n log log n) operations and O(sqrt(n)) space.
/// @param limit Sieve primes <= limit.
///
void segmented_sieve(int64_t limit)
{
int64_t sqrt = (int64_t) std::sqrt(limit);
int64_t segment_size = std::max(sqrt, L1D_CACHE_SIZE);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment