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);
  }
}


Go: package main

import (
    "fmt"
    "github.com/shabbyrobe/go-num"
    "strings"
    "time"
)

func b10(n int64) {
    if n == 1 {
        fmt.Printf("%4d: %28s  %-24d\n", 1, "1", 1)
        return
    }
    n1 := n + 1
    pow := make([]int64, n1)
    val := make([]int64, n1)
    var count, ten, x int64 = 0, 1, 1
    for ; x < n1; x++ {
        val[x] = ten
        for j := int64(0); j < n1; j++ {
            if pow[j] != 0 && pow[(j+ten)%n] == 0 && pow[j] != x {
                pow[(j+ten)%n] = x
            }
        }
        if pow[ten] == 0 {
            pow[ten] = x
        }
        ten = (10 * ten) % n
        if pow[0] != 0 {
            break
        }
    }
    x = n
    if pow[0] != 0 {
        s := ""
        for x != 0 {
            p := pow[x%n]
            if count > p {
                s += strings.Repeat("0", int(count-p))
            }
            count = p - 1
            s += "1"
            x = (n + x - val[p]) % n
        }
        if count > 0 {
            s += strings.Repeat("0", int(count))
        }
        mpm := num.MustI128FromString(s)
        mul := mpm.Quo64(n)
        fmt.Printf("%4d: %28s  %-24d\n", n, s, mul)
    } else {
        fmt.Println("Can't do it!")
    }
}

func main() {
    start := time.Now()
    tests := [][]int64{{1, 10}, {95, 105}, {297}, {576}, {594}, {891}, {909}, {999},
        {1998}, {2079}, {2251}, {2277}, {2439}, {2997}, {4878}}
    fmt.Println("   n                           B10  multiplier")
    fmt.Println("----------------------------------------------")
    for _, test := range tests {
        from := test[0]
        to := from
        if len(test) == 2 {
            to = test[1]
        }
        for n := from; n <= to; n++ {
            b10(n)
        }
    }
    fmt.Printf("\nTook %s\n", time.Since(start))
}