Task: Klarner-Rado sequence

C: #include <stdio.h>

#define ELEMENTS 10000000U

void make_klarner_rado(unsigned int *dst, unsigned int n) {
    unsigned int i, i2 = 0, i3 = 0;
    unsigned int m, m2 = 1, m3 = 1;

    for (i = 0; i < n; ++i) {
        dst[i] = m = m2 < m3 ? m2 : m3;
        if (m2 == m) m2 = dst[i2++] << 1 | 1;
        if (m3 == m) m3 = dst[i3++] * 3 + 1;
    }
}

int main(void) {
    static unsigned int klarner_rado[ELEMENTS];
    unsigned int i;

    make_klarner_rado(klarner_rado, ELEMENTS);

    for (i = 0; i < 99; ++i)
        printf("%u ", klarner_rado[i]);
    for (i = 100; i <= ELEMENTS; i *= 10)
        printf("%u\n", klarner_rado[i - 1]);

    return 0;
}








VB: Option Strict On
Option Explicit On

Imports System.IO

Module KlarnerRado

    Private Const bitsWidth As Integer = 31

    Private bitMask() As Integer = _
        New Integer(){ 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024 _
                     , 2048, 4096, 8192, 16384, 32768, 65536, 131072 _
                     , 262144, 524288, 1048576, 2097152, 4194304, 8388608 _
                     , 16777216, 33554432, 67108864, 134217728, 268435456 _
                     , 536870912, 1073741824 _
                     }

    Private Const maxElement As Integer = 1100000000


    Private Function BitSet(bit As Integer, v As Integer) As Boolean
        Return (v And bitMask(bit - 1)) <> 0
    End Function

    Private Function SetBit(ByVal bit As Integer, ByVal b As Integer) As Integer
        Return b Or bitMask(bit - 1)
    End Function

    Public Sub Main
        Dim  kr(maxElement \ bitsWidth) As Integer

        For i As Integer = 0 To kr.Count() - 1
            kr(i) = 0
        Next  i

        Dim krCount As Integer =  0    
        Dim n21 As Integer = 3         
        Dim n31 As Integer = 4         
        Dim p10 As Integer = 1000      
        Dim iBit As Integer = 0        
        Dim iOverBw As Integer = 0     
        
        Dim p2Bit As Integer = 1       
        Dim p2OverBw As Integer = 0    
        
        Dim p3Bit As Integer = 1       
        Dim p3OverBw As Integer = 0    

        kr(0) = SetBit(1, kr(0))
        Dim kri As Boolean = True
        Dim lastI As Integer = 0
        For i As Integer = 1 To  maxElement
            iBit += 1
            If iBit > bitsWidth Then
                iBit = 1
                iOverBw += 1
            End If
            If i = n21 Then            
                If BitSet(p2Bit, kr(p2OverBw)) Then
                    kri = True
                End If
                p2Bit += 1
                If p2Bit > bitsWidth Then
                    p2Bit = 1
                    p2OverBw += 1
                End If
                n21 += 2
            End If
            If i = n31 Then            
                If BitSet(p3Bit, kr(p3OverBw)) Then
                    kri = True
                End If
                p3Bit += 1
                If p3Bit > bitsWidth Then
                    p3Bit = 1
                    p3OverBw += 1
                End If
                n31 += 3
            End If
            If kri Then
                lastI = i
                kr(iOverBw) = SetBit(iBit, kr(iOverBw))
                krCount += 1
                If krCount <= 100 Then
                    Console.Out.Write(" " & i.ToString().PadLeft(3))
                    If krCount Mod 20 = 0 Then
                        Console.Out.WriteLine()
                    End If
                ElseIf krCount = p10 Then
                    Console.Out.WriteLine("Element " & p10.ToString().PadLeft(10) & " is " & i.ToString().PadLeft(10))
                    p10 *= 10
                End If
                kri = False
            End If
        Next  i
        Console.Out.WriteLine("Element " & krCount.ToString().PadLeft(10) & " is " & lastI.ToString().PadLeft(10))

    End Sub

End Module