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;
}








VB: Imports DT = System.DateTime

Module Module1

    Iterator Function Primes(lim As Integer) As IEnumerable(Of Integer)
        Dim flags(lim) As Boolean

        Dim j = 2

        Dim d = 3
        Dim sq = 4
        While sq <= lim
            If Not flags(j) Then
                Yield j
                For k = sq To lim Step j
                    flags(k) = True
                Next
            End If

            j += 1
            d += 2
            sq += d
        End While

        While j <= lim
            If Not flags(j) Then
                Yield j
            End If
            j += 1
        End While
    End Function

    Sub Main()
        For Each lmt In {90, 300, 3000, 30000, 111000}
            Dim pr = Primes(lmt).Skip(1).ToList()
            Dim st = DT.Now
            Dim f = 0
            Dim r As New List(Of String)
            Dim i = -1
            Dim m = lmt \ 3
            Dim h = m
            While i < 0
                i = pr.IndexOf(h)
                h -= 1
            End While
            Dim j = i - 1
            Dim k = j - 1
            For a = 0 To k
                Dim pra = pr(a)
                For b = a + 1 To j
                    Dim prab = pra + pr(b)
                    For c = b + 1 To i
                        Dim d = prab + pr(c)
                        If Not pr.Contains(d) Then
                            Continue For
                        End If
                        f += 1
                        If lmt < 100 Then
                            r.Add(String.Format("{3,5} = {0,2} + {1,2} + {2,2}", pra, pr(b), pr(c), d))
                        End If
                    Next
                Next
            Next
            Dim s = "s.u.p.t.s under "
            r.Sort()
            If r.Count > 0 Then
                Console.WriteLine("{0}{1}:" + vbNewLine + "{2}", s, m, String.Join(vbNewLine, r))
            End If
            If lmt > 100 Then
                Console.WriteLine("Count of {0}{1,6:n0}: {2,13:n0}  {3} sec", s, m, f, (DT.Now - st).ToString().Substring(6))
            End If
        Next
    End Sub

End Module