Task: Magnanimous numbers

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

typedef int bool;
typedef unsigned long long ull;

#define TRUE 1
#define FALSE 0


bool is_prime(ull n) {
    ull d;
    if (n < 2) return FALSE;
    if (!(n % 2)) return n == 2;
    if (!(n % 3)) return n == 3;
    d = 5;
    while (d * d <= n) {
        if (!(n % d)) return FALSE;
        d += 2;
        if (!(n % d)) return FALSE;
        d += 4;
    }
    return TRUE;
}

void ord(char *res, int n) {
    char suffix[3];
    int m = n % 100;
    if (m >= 4 && m <= 20) {
        sprintf(res,"%dth", n);
        return;
    }
    switch(m % 10) {
        case 1:
            strcpy(suffix, "st");
            break;
        case 2:
            strcpy(suffix, "nd");
            break;
        case 3:
            strcpy(suffix, "rd");
            break;
        default:
            strcpy(suffix, "th");
            break;
    }
    sprintf(res, "%d%s", n, suffix);
}

bool is_magnanimous(ull n) {
    ull p, q, r;
    if (n < 10) return TRUE;
    for (p = 10; ; p *= 10) {
        q = n / p;
        r = n % p;
        if (!is_prime(q + r)) return FALSE;
        if (q < 10) break;
    }
    return TRUE;
}

void list_mags(int from, int thru, int digs, int per_line) {
    ull i = 0;
    int c = 0;
    char res1[13], res2[13];
    if (from < 2) {
        printf("\nFirst %d magnanimous numbers:\n", thru);
    } else {
        ord(res1, from);
        ord(res2, thru);
        printf("\n%s through %s magnanimous numbers:\n", res1, res2);
    }
    for ( ; c < thru; ++i) {
        if (is_magnanimous(i)) {
            if (++c >= from) {
                printf("%*llu ", digs, i);
                if (!(c % per_line)) printf("\n");
            }
        }
    }
}
 
int main() {
    list_mags(1, 45, 3, 15);
    list_mags(241, 250, 1, 10);
    list_mags(391, 400, 1, 10);
    return 0;
}








VB: Imports System, System.Console

Module Module1

    Dim np As Boolean()

    Sub ms(ByVal lmt As Long)
        np = New Boolean(CInt(lmt)) {} : np(0) = True : np(1) = True
        Dim n As Integer = 2, j As Integer = 1 : While n < lmt
            If Not np(n) Then
                Dim k As Long = CLng(n) * n
                While k < lmt : np(CInt(k)) = True : k += n : End While
            End If : n += j : j = 2 : End While
    End Sub

    Function is_Mag(ByVal n As Integer) As Boolean
        Dim res, rm As Integer, p As Integer = 10
        While n >= p
            res = Math.DivRem(n, p, rm)
            If np(res + rm) Then Return False
            p = p * 10 : End While : Return True
    End Function

    Sub Main(ByVal args As String())
        ms(100_009) : Dim mn As String = " magnanimous numbers:"
        WriteLine("First 45{0}", mn) : Dim l As Integer = 0, c As Integer = 0
        While c < 400 : If is_Mag(l) Then
            c += 1 : If c <= 45 OrElse (c > 240 AndAlso c <= 250) OrElse c > 390 Then Write(If(c <= 45, "{0,4} ", "{0,8:n0} "), l)
            If c < 45 AndAlso c Mod 15 = 0 Then WriteLine()
            If c = 240 Then WriteLine(vbLf & vbLf & "241st through 250th{0}", mn)
            If c = 390 Then WriteLine(vbLf & vbLf & "391st through 400th{0}", mn)
        End If : l += 1 : End While
    End Sub
End Module