Task: Minimum positive multiple in base 10 using only 0 and 1



C#: using System;
using System.Collections.Generic;
using static System.Console;
 
class Program {
 
  static string B10(int n) {
    int[] pow = new int[n + 1], val = new int[29];
    for (int count = 0, ten = 1, x = 1; x <= n; x++) {
      val[x] = ten;
      for (int j = 0, t; j <= n; j++)
        if (pow[j] != 0 && pow[j] != x && pow[t = (j + ten) % n] == 0)
          pow[t] = x;
      if (pow[ten] == 0) pow[ten] = x;
      ten = (10 * ten) % n;
      if (pow[0] != 0) {
        x = n;
        string s = ""; 
        while (x != 0) {
          int p = pow[x % n];
          if (count > p) s += new string('0', count - p);
          count = p - 1;
          s += "1";
          x = (n + x - val[p]) % n;
        }
        if (count > 0) s += new string('0', count);
        return s;
      }
    } 
    return "1";
  }
 
  static void Main(string[] args) {
    string fmt = "{0,4} * {1,24} = {2,-28}\n";
    int[] m = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
      95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105,
      297, 576, 594, 891, 909, 999, 1998, 2079, 2251, 2277, 2439, 2997, 4878 };
    string[] r = new string[m.Length]; 
    WriteLine(fmt + new string('-', 62), "n", "multiplier", "B10");
    var sw = System.Diagnostics.Stopwatch.StartNew();
    for (int i = 0; i < m.Length; i++) r[i] = B10(m[i]);
    sw.Stop();
    for (int i = 0; i < m.Length; i++) Write(fmt, m[i], decimal.Parse(r[i]) / m[i], r[i]);
    Write("\nTook {0}ms", sw.Elapsed.TotalMilliseconds);
  }
}






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

Module Module1

    Dim fmt As String = "{0,4} * {1,31:n0} = {2,-28}" & vbLf

    Sub B10(ByVal n As Integer)
        If n <= 1 Then Return
        Dim pow As Integer() = New Integer(n) {},
            val As Integer() = New Integer(n) {},
            count As Integer = 0, ten As Integer = 1, x As Integer = 1
        While x <= n : val(x) = ten : For j As Integer = 0 To n
                If pow(j) <> 0 AndAlso pow((j + ten) Mod n) = 0 AndAlso _
                   pow(j) <> x Then pow((j + ten) Mod n) = x
            Next : If pow(ten) = 0 Then pow(ten) = x
            ten = (10 * ten) Mod n : If pow(0) <> 0 Then
                x = n : Dim s As String = "" : While x <> 0
                    Dim p As Integer = pow(x Mod n)
                    If count > p Then s += New String("0"c, count - p)
                    count = p - 1 : s += "1"
                    x = (n + x - val(p)) Mod n : End While
                If count > 0 Then s += New String("0"c, count)
                Write(fmt, n, Decimal.Parse(s) / n, s) : Return
            End If : x += 1 : End While : Write("Can
    End Sub

    Sub Main(ByVal args As String())
        Dim st As DateTime = DateTime.Now
        Dim m As List(Of Integer) = New List(Of Integer) From _
            {297,576,594,891,909,999,1998,2079,2251,2277,2439,2997,4878}
        Write(fmt, "n", "multiplier", "B10")
        WriteLine(New String("-"c, 69)) : Write(fmt, 1, 1, 1)
        For n As Integer = 1 To m.Last()
            If n <= 10 OrElse n >= 95 AndAlso n <= 105 OrElse m.Contains(n) Then B10(n)
        Next : WriteLine(vbLf & "Took {0}ms", (DateTime.Now - st).TotalMilliseconds)
    End Sub
End Module