Task: Strange unique prime triplets

C: #include <stdbool.h>
#include <stdio.h>
#include <string.h>

#define LIMIT 3000

void init_sieve(unsigned char sieve[], int limit) {
    int i, j;

    for (i = 0; i < limit; i++) {
        sieve[i] = 1;
    }
    sieve[0] = 0;
    sieve[1] = 0;

    for (i = 2; i < limit; i++) {
        if (sieve[i]) {
            for (j = i + i; j < limit; j += i) {
                sieve[j] = 0;
            }
        }
    }
}

void strange_unique_prime_triplets(unsigned char sieve[], int limit, bool verbose) {
    int count = 0, sum;
    int i, j, k, n, p;
    int pi, pj, pk;

    n = 0;
    for (i = 0; i < limit; i++) {
        if (sieve[i]) {
            n++;
        }
    }

    if (verbose) {
        printf("Strange unique prime triplets < %d:\n", limit);
    }

    for (i = 0; i + 2 < n; i++) {
        pi = 2;
        p = i;
        while (p > 0) {
            pi++;
            if (sieve[pi]) {
                p--;
            }
        }

        for (j = i + 1; j + 1 < n; j++) {
            pj = pi;
            p = j - i;
            while (p > 0) {
                pj++;
                if (sieve[pj]) {
                    p--;
                }
            }

            for (k = j + 1; k < n; k++) {
                pk = pj;
                p = k - j;
                while (p > 0) {
                    pk++;
                    if (sieve[pk]) {
                        p--;
                    }
                }

                sum = pi + pj + pk;
                if (sum < LIMIT && sieve[sum]) {
                    count++;
                    if (verbose) {
                        printf("%2d + %2d + %2d = %d\n", pi, pj, pk, sum);
                    }
                }
            }
        }
    }

    printf("Count of strange unique prime triplets < %d is %d.\n\n", limit, count);
}

int main() {
    unsigned char sieve[LIMIT];

    init_sieve(sieve, LIMIT);

    strange_unique_prime_triplets(sieve, 30, true);
    strange_unique_prime_triplets(sieve, 1000, false);

    return 0;
}


C++: #include <iomanip>
#include <iostream>
#include <vector>

std::vector<bool> prime_sieve(size_t limit) {
    std::vector<bool> sieve(limit, true);
    if (limit > 0)
        sieve[0] = false;
    if (limit > 1)
        sieve[1] = false;
    for (size_t i = 4; i < limit; i += 2)
        sieve[i] = false;
    for (size_t p = 3; ; p += 2) {
        size_t q = p * p;
        if (q >= limit)
            break;
        if (sieve[p]) {
            size_t inc = 2 * p;
            for (; q < limit; q += inc)
                sieve[q] = false;
        }
    }
    return sieve;
}

void strange_unique_prime_triplets(int limit, bool verbose) {
    std::vector<bool> sieve = prime_sieve(limit * 3);
    std::vector<int> primes;
    for (int p = 3; p < limit; p += 2) {
        if (sieve[p])
            primes.push_back(p);
    }
    size_t n = primes.size();
    size_t count = 0;
    if (verbose)
        std::cout << "Strange unique prime triplets < " << limit << ":\n";
    for (size_t i = 0; i + 2 < n; ++i) {
        for (size_t j = i + 1; j + 1 < n; ++j) {
            for (size_t k = j + 1; k < n; ++k) {
                int sum = primes[i] + primes[j] + primes[k];
                if (sieve[sum]) {
                    ++count;
                    if (verbose) {
                        std::cout << std::setw(2) << primes[i] << " + "
                                  << std::setw(2) << primes[j] << " + "
                                  << std::setw(2) << primes[k] << " = " << sum
                                  << '\n';
                    }
                }
            }
        }
    }
    std::cout << "\nCount of strange unique prime triplets < " << limit
              << " is " << count << ".\n";
}

int main() {
    strange_unique_prime_triplets(30, true);
    strange_unique_prime_triplets(1000, false);
    return 0;
}