Skip to content

Instantly share code, notes, and snippets.

@Animeshz
Forked from rongjiecomputer/prime.cpp
Created January 7, 2022 11:50
Show Gist options
  • Select an option

  • Save Animeshz/d4cc109cd582046d50446498d5246506 to your computer and use it in GitHub Desktop.

Select an option

Save Animeshz/d4cc109cd582046d50446498d5246506 to your computer and use it in GitHub Desktop.

Revisions

  1. @rongjiecomputer rongjiecomputer created this gist Apr 5, 2017.
    22 changes: 22 additions & 0 deletions prime.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    #include <stdio.h>

    template <size_t N>
    struct PrimeTable {
    constexpr PrimeTable() : sieve() {
    sieve[0] = sieve[1] = false;
    for (size_t i = 2; i < N; i++) sieve[i] = true;
    for (size_t i = 2; i < N; i++) {
    if (sieve[i])
    for (size_t j = i*i; j < N; j += i) sieve[j] = false;
    }
    }
    bool sieve[N];
    };

    int main() {
    constexpr auto table = PrimeTable<100000>();
    for (int i = 0; i < 20; i++) {
    printf("isPrime(%d): %d\n", i, table.sieve[i]);
    }
    return 0;
    }