Task: Long primes

C: #include <stdio.h>
#include <stdlib.h>
 
#define TRUE 1
#define FALSE 0
 
typedef int bool;
 
void sieve(int limit, int primes[], int *count) {
    bool *c = calloc(limit + 1, sizeof(bool)); 
    
    int i, p = 3, p2, n = 0;
    p2 = p * p;
    while (p2 <= limit) {
        for (i = p2; i <= limit; i += 2 * p) 
            c[i] = TRUE;
        do {
            p += 2;
        } while (c[p]);
        p2 = p * p;
    }
    for (i = 3; i <= limit; i += 2) {
        if (!c[i]) primes[n++] = i;
    }
    *count = n;
    free(c);
}
 

int findPeriod(int n) {
    int i, r = 1, rr, period = 0;
    for (i = 1; i <= n + 1; ++i) {
        r = (10 * r) % n;
    }
    rr = r;
    do {
        r = (10 * r) % n;
        period++;
    } while (r != rr);
    return period;
}
 
int main() {
    int i, prime, count = 0, index = 0, primeCount, longCount = 0, numberCount;
    int *primes, *longPrimes, *totals;
    int numbers[] = {500, 1000, 2000, 4000, 8000, 16000, 32000, 64000};
     
    primes = calloc(6500, sizeof(int));
    numberCount = sizeof(numbers) / sizeof(int);
    totals = calloc(numberCount, sizeof(int));
    sieve(64000, primes, &primeCount);
    longPrimes = calloc(primeCount, sizeof(int)); 
    
    for (i = 0; i < primeCount; ++i) {
        prime = primes[i];
        if (findPeriod(prime) == prime - 1) {
            longPrimes[longCount++] = prime;
        }
    }
    for (i = 0; i < longCount; ++i, ++count) {
        if (longPrimes[i] > numbers[index]) {
            totals[index++] = count;
        }
    }
    totals[numberCount - 1] = count;
    printf("The long primes up to %d are:\n", numbers[0]);
    printf("[");
    for (i = 0; i < totals[0]; ++i) {
        printf("%d ", longPrimes[i]);
    }
    printf("\b]\n");
 
    printf("\nThe number of long primes up to:\n");
    for (i = 0; i < 8; ++i) {
        printf("  %5d is %d\n", numbers[i], totals[i]);
    }
    free(totals);
    free(longPrimes);
    free(primes);
    return 0;
}








VB: Imports System, System.Collections.Generic, System.Linq, System.Console

Module LongPrimes

    Function Period(ByVal n As Integer) As Integer
        Dim m As Integer, r As Integer = 1
        For i As Integer = 0 To n : r = 10 * r Mod n : Next
        m = r : Period = 1 : While True
            r = (10 * r) Mod n : If r = m Then Return Period
            Period += 1 : End While
    End Function

    Sub Main()
        Dim primes As IEnumerable(Of Integer) = SomePrimeGenerator.Primes(64000).Skip(1).Where(Function(p) Period(p) = p - 1).Append(99999)
        Dim count As Integer = 0, limit As Integer = 500
        WriteLine(String.Join(" ", primes.TakeWhile(Function(p) p <= limit)))
        For Each prime As Integer In primes
            If prime > limit Then
                WriteLine($"There are {count} long primes below {limit}")
                limit <<= 1 : End If : count += 1 : Next
    End Sub

End Module

Module SomePrimeGenerator

    Iterator Function Primes(lim As Integer) As IEnumerable(Of Integer)
        Dim flags As Boolean() = New Boolean(lim) {},
            j As Integer = 2, d As Integer = 3, sq As Integer = 4
        While sq <= lim
            If Not flags(j) Then
                Yield j : For k As Integer = 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
            j += 1 : End While
    End Function

End Module