Task: Humble numbers



C#: using System;
using System.Collections.Generic;

namespace HumbleNumbers {
    class Program {
        static bool IsHumble(int i) {
            if (i <= 1) return true;
            if (i % 2 == 0) return IsHumble(i / 2);
            if (i % 3 == 0) return IsHumble(i / 3);
            if (i % 5 == 0) return IsHumble(i / 5);
            if (i % 7 == 0) return IsHumble(i / 7);
            return false;
        }

        static void Main() {
            var limit = short.MaxValue;
            Dictionary<int, int> humble = new Dictionary<int, int>();
            var count = 0;
            var num = 1;

            while (count < limit) {
                if (IsHumble(num)) {
                    var str = num.ToString();
                    var len = str.Length;
                    if (humble.ContainsKey(len)) {
                        humble[len]++;
                    } else {
                        humble[len] = 1;
                    }
                    if (count < 50) Console.Write("{0} ", num);
                    count++;
                }
                num++;
            }
            Console.WriteLine("\n");

            Console.WriteLine("Of the first {0} humble numbers:", count);
            num = 1;
            while (num < humble.Count - 1) {
                if (humble.ContainsKey(num)) {
                    var c = humble[num];
                    Console.WriteLine("{0,5} have {1,2} digits", c, num);
                    num++;
                } else {
                    break;
                }
            }
        }
    }
}






VB: Module Module1

    Function IsHumble(i As Long) As Boolean
        If i <= 1 Then
            Return True
        End If
        If i Mod 2 = 0 Then
            Return IsHumble(i \ 2)
        End If
        If i Mod 3 = 0 Then
            Return IsHumble(i \ 3)
        End If
        If i Mod 5 = 0 Then
            Return IsHumble(i \ 5)
        End If
        If i Mod 7 = 0 Then
            Return IsHumble(i \ 7)
        End If
        Return False
    End Function

    Sub Main()
        Dim LIMIT = Short.MaxValue
        Dim humble As New Dictionary(Of Integer, Integer)
        Dim count = 0L
        Dim num = 1L

        While count < LIMIT
            If (IsHumble(num)) Then
                Dim str = num.ToString
                Dim len = str.Length
                If len > 10 Then
                    Exit While
                End If
                If humble.ContainsKey(len) Then
                    humble(len) += 1
                Else
                    humble(len) = 1
                End If
                If count < 50 Then
                    Console.Write("{0} ", num)
                End If
                count += 1
            End If
            num += 1
        End While
        Console.WriteLine(vbNewLine)

        Console.WriteLine("Of the first {0} humble numbers:", count)
        num = 1
        While num < humble.Count
            If humble.ContainsKey(num) Then
                Dim c = humble(num)
                Console.WriteLine("{0,5} have {1,2} digits", c, num)
                num += 1
            Else
                Exit While
            End If
        End While
    End Sub

End Module