Trova un multiplo di un determinato numero la cui rappresentazione decimale sembra binaria


34

Ho trovato una domanda sul sito di Code Review che sembra interessante. Penso che OP stia sbagliando, ma non posso esserne sicuro ... Quindi risolviamolo per lui! (scrivere un programma, non una funzione / procedura)

Input (stdin o simile):

Un numero intero xin notazione decimale. È maggiore di 1 e inferiore a 2 ^ 31.

Uscita (stdout o simile):

Un numero intero yin notazione decimale. Il prodotto x * ynella rappresentazione decimale deve contenere solo cifre 0 e 1. Deve essere il numero minimo tale maggiore di 0.

Nota: l'output non è limitato - se il minimo yè circa 10 ^ 100, il programma deve emettere tutte le sue 100 cifre (non so se esiste un limite ragionevole, come 2 ^ 64, on y- non lo ha risolto) ).

Il tuo programma dovrebbe concludersi in un tempo ragionevole (1 secondo? 1 ora? - qualcosa del genere) per tutti xnella gamma.

Bonus:

Se il tuo programma non ha un limite sulla dimensione dell'input (eccetto la RAM) e ha una complessità polinomiale, moltiplica il numero di byte del tuo programma 0.8e arrotondalo per difetto.


Esempio: input 2; output 5, perché 2 * 5 = 10

Esempio: input 21; output 481, perché 21 * 481 = 10101


Disclaimer: non sono responsabile per la domanda sul sito di revisione del codice. In caso di discrepanze, solo la descrizione sopra deve essere considerata come specifica adeguata.

OEIS A079339


6
Dovrebbe essere sempre risolvibile. Chiaramente deve esistere almeno una q tale che vi sia un numero infinito di n tale che 10 ^ n mod x = q. Prendi x tali valori di n e somma i rispettivi poteri 10 ^ n.
feersum

1
I multipli di 9 sembrano produrre risultati insolitamente alti.
SuperJedi224,

1
Progetto correlato Problema di Eulero , per chiunque pensasse che questa domanda fosse familiare
Sp3000,

1
Per complessità polinomiale, intendi polinomio nel numero di cifre dell'input o polinomio nel valore dell'input?
Reto Koradi,

3
La mia @anatolyg non è una forza bruta
aditsu,

Risposte:


8

Pyth, 9 byte

f!-`*TQ10

Dimostrazione

Per ogni multiplo, converti in una stringa, sottrai le cifre 10(usando il pratico int di Pyth per eseguire il cast in questo caso) e quindi nega logicamente il risultato, terminando la ricerca solo quando viene trovato il multiplo corretto.

Soluzione bonus, 10 byte:

f.xi`*TQ2Z

Questa soluzione controlla effettivamente se la rappresentazione di stringa del numero può essere trattata come un numero binario ( i ... 2) e termina quando non viene generato un errore in questo tentativo.


18

Python 2, soluzione efficiente, 99

n=input()
d={n:0}
k=1
while min(d):[d.setdefault((x+k)%n,d[x]+k)for x in set(d)];k*=10
print d[0]/n

Grazie Sp3000 per alcuni suggerimenti sul golf.

Sfido tutti gli altri a pubblicare (nelle proprie risposte) quanto tempo ci vuole per ottenere il risultato per l'input 72o 99:) Se quelli sono davvero veloci, prova qualcosa di simile al 79992prossimo (ancora <1 secondo qui).

Spiegazione:

Ho pensato che non fosse necessario (dato che il codice è abbastanza leggibile), ma ho ricevuto una richiesta, quindi eccolo qui:

La prima idea è che un numero dall'aspetto binario sia una somma di 1 o più potenze diverse di 10. Pertanto, possiamo provare ad aggiungere varie potenze di 10 in modi diversi fino ad ottenere il resto 0.

Se lo facciamo in modo ingenuo, è lo stesso che generare tutti i numeri dall'aspetto binario e testarli. Ma molti resti saranno gli stessi. Un modo migliore è registrare solo il numero più piccolo che ha dato un certo resto, e successivamente aggiungere maggiori potenze di 10 ai numeri che abbiamo registrato. Questo è quello che fa il programma.

dè un dizionario / mappa in cui le chiavi sono i resti e i valori sono numeri dall'aspetto binario con quel resto. L'iniziale n:0è un caso speciale: dovrebbe essere 0:0così che possiamo iniziare ad aggiungere poteri ad esso, ma l'algoritmo si interrompe quando trova la chiave 0, quindi ho usato ninvece, che è garantito per avere lo stesso effetto e non interferire con gli altri valori.

Quindi iniziamo ad aggiungere poteri di 10 (memorizzati k) a tutti i numeri esistenti e registrando i resti. Aggiungiamo kal resto: (x+k)%ne al numero:, d[x]+ke lo registriamo solo se è un nuovo resto:, d.setdefault(…)quindi passiamo alla potenza successiva: k*=10e ripetiamo fino a quando non otteniamo la chiave 0:while min(d)

Alla fine, d[0]dà il numero binario che ha resto 0 mod n, quindi lo dividiamo nper ottenere la soluzione.

Nota: il programma può essere reso più efficiente evitando numeri grandi (registrando esponenti invece di potenze di 10 e calcolando i resti di potenze da valori precedenti), ma è codice golf, quindi ...

In effetti, qui, ho scritto una versione più veloce:

n=input()
d={n:0}
k=1
b=0
while 0not in d:
 for x in list(d):d.setdefault((x+k)%n,b)
 k=(k*10)%n;b+=1
x=10**d[0]
while x%n:x+=10**d[n-x%n]
print x/n

1
Neanche la mia risposta. xD "Dangit, Java, maledici la tua scelta di Integer.MAX_VALUE rispetto all'uso di BigInteger di default!" - Ogni programmatore Java di sempre
Addison Crump,

@VTCAKAVSMoACE perché non usi Long?
aditsu,

Hmm. È un byte in più, ma ... ne vale la pena. Grazie!
Addison Crump,

O no. Questo in realtà lo riduce seriamente. Grazie!
Addison Crump,

1
Tempi per la risoluzione di 99: aditsu: 0,001 secondi; xnor: 5+ ore e non è stato ancora completato.
user193661,

13

Python 2, 47 byte

n=a=input()
while'1'<max(str(a)):a+=n
print a/n

Tiene traccia del numero di input ne del multiplo corrente a. Quando aappare come binario, genera il rapporto a/n. Per verificare che un numero sia composto da 0"se 1", confrontiamo il carattere massimo nella sua rappresentazione di stringa con '1'.

Usa str(a)invece di `a`evitare che i long finiscano L. Sfortunatamente, 'L'è più grande di '1'.


12

Perl, 27 byte

#!perl -p
1while($_*++$\)=~/[2-9]/}{

Contando lo shebang come uno, l'input è preso dallo stdin.

Esempio di utilizzo

$ echo 2 | perl dec-bin.pl
5

$ echo 21 | perl dec-bin.pl
481

$ echo 98 | perl dec-bin.pl
112245

Perl, 25 byte

#!perl -p
eval'0b'.++$\*$_||redo}{

Un miglioramento a due byte di @skmrx .

Invece di verificare un regex, questo tenta invece di valutare il prodotto come un valore binario letterale. In caso di fallimento, passa al successivo. In genere, la octfunzione viene utilizzata per questo scopo, ma regola automaticamente cifre non valide, il che non è utile in questa sfida.


Perl, 40 byte

#!perl -p
1while($b=sprintf"%b",++$i)%$_;$_=$b/$_

Una soluzione molto più efficiente. Esaminiamo le rappresentazioni binarie, le interpretiamo come base 10 e quindi controlliamo la divisibilità. I tempi di esecuzione per tutti i valori inferiori a 100 sono trascurabili.

Esempio di utilizzo

$ echo 72|perl dec-bin.pl
1543209875

$ echo 99|perl dec-bin.pl
1122334455667789

2
Bello :) Ho imparato un paio di cose nuove dal tuo post oggi! Durante la lettura del tuo codice, ho trovato un modo per radere via un paio di byte dal primo codice:eval"0b".$_*++$\||redo}{
svsd

Ma suppongo che dovremo includere use bigintper supportare i grandi numeri che OP ha richiesto di essere supportato :(
svsd

1
@skmrn È geniale. Ci avevo provato oct'0b'.++$\*$_, ma taglia silenziosamente cifre non valide. Non pensavo di usare evalinvece.
primo

11

Javascript, 43 byte

Questo è finito molto più breve di quanto pensassi. In pratica aumenta di y1 fino a y * (input number) = (binary-looking number). Ovviamente abbastanza inefficiente.

for(x=prompt(y=0);!+('0b'+x*++y););alert(y)


Javascript (soluzione più efficiente), 53 byte

Questo aumenta yin binario fino a y / (input number) = (number without a remainder). Quindi esce (number without a remainder).

for(x=prompt(y=1);(z=y.toString(2))%x;y++);alert(z/x)


Javascript (soluzione ancora più efficiente), 76 byte

Questo combina entrambi i metodi precedenti sopra descritti. Controlla gli incrementi yfino a quando y * (input number) = (binary-looking number)(significa che l'uscita è y) OPPURE y / (input number) = (number without a remainder)(significa che l'uscita è (number without a remainder)).

for(x=prompt(y=a=0);!a;a=+('0b'+x*++y)?y:(z=y.toString(2))%x?0:z/x);alert(a)


Dovrebbe dare 1 quando possibile (esempio di input: 1)
edc65

@ edc65 Risolto - senza cambiare il conteggio dei byte!
Mama Fun Roll

Ciò provoca l'arresto anomalo di Safari 9.0. Jussayin'. :)
Addison Crump,

1
Ma è limitato a piccoli numeri in uscita. I numeri Javascript hanno 17 cifre di precisione, OP richiede qualcosa di più grande (e può essere fatto usando l'aritmetica modulare)
edc65

Protip: non provare a inserire 72. Firefox 41 si blocca per 15 minuti e poi si arresta in modo anomalo. L'ho scoperto nel modo più duro.
ETHproductions

9

Haskell, 72 70 64 60 58 byte

main=do x<-readLn;print$[y|y<-[1..],all(<'2')$show$x*y]!!0

Modifica: @Jan Dvorak mi ha aiutato a salvare 4 byte.

Modifica: @BlackCap ha salvato 2 byte passando alla donotazione. Grazie!


main=print.f=<<readLn
John Dvorak,

È possibile salvare un byte inserendo f:main=readLn>>= \x->print$[y|y<-[1..],all(<'2')$show$x*y]!!0
BlackCap

2 in realtàmain=do x<-readLn;print$[y|y<-[1..],all(<'2')$show$x*y]!!0
BlackCap

@BlackCap: Nice! Molte grazie!
nimi,

7

Python 2, 67 65 63 60 byte

a=input();b=1
while set(`a*b`)&set('23456789'):b+=1
print b

Grazie a Status per 2 byte e Shebang per 5 byte!


1
Penso che devi inizializzareb=1
anatolyg

2
Puoi radere 2 byte facendoany(c in`a*b`for c in'23456789')
Stato

1
Non ne sono sicuro ma not c in`a*b`for c in'10'funzionerebbe?
Cole

2
È possibile salvare 6 byte modificando la condizione while in set('a*b')&set('23456789').
Kade,

2
`produce una La lungo e 'L'>'1'.
user193661,

6

JavaScript (ES6) 222 250

Utilizzo di matematica di precisione arbitraria (operando su stringhe di cifre decimali)

Questo può essere giocato un po 'di più (fatto), ma mi piace il fatto che non sia limitato ai numeri standard JS (17 cifre decimali di precisione) e che sia veloce.

Prova a eseguire lo snippet di seguito in un browser compatibile con EcmaScript 6. Il tempo è accettabile fino a 9998 - non provare 9999 ed essere paziente con 999.

// As a complete program with I/O via popup  
for(n=+prompt(a=[0],q=[t=1]);t;){for(c=1,t=i=0;i<a.length;i++)a[i]=a[i]&c?0:a[i]|c?(c=0,t+=q[i],1):c=0;c&&(a[i]=c,t+=q[i]=q[i-1]*10%n);t%=n}a.reverse().map(a=>(z+=[a],d=z/n|0,z%=n,r||d?r+=d:0),r='',z=0);alert([r,a.join``])

// As a testable function
f=n=>{
  for(a=[0],q=[t=1];t;)
  {
    for(c=1,t=i=0;i<a.length;i++)
      a[i]=a[i]&c?0:a[i]|c?(c=0,t+=q[i],1):c=0
    c&&(a[i]=c,t+=q[i]=q[i-1]*10%n);
    t%=n
  }  
  a.reverse().map(a=>(z+=[a],d=z/n|0,z%=n,r||d?r+=d:0),r='',z=0)
  return [r,a.join``]
}

// Test and timing
out = x => O.innerHTML += x + '\n'

setTimeout(_=>{
;[1,2,10, 21, 23, 98, 72, 9, 99, 999]
.forEach((test,i) => { 
  var t0 = ~new Date  
  var result = f(test)
  out('n='+test+' '+result+' time(ms) ' + (t0-~new Date))
})},100)  
<pre id=O>Timing test cases ...
</pre>

Più leggibile

Questa è la prima versione, con modulo e lunga divisione come funzioni separate.

// function M - Modulus with arbitrary precision - a is a string of decimal digits
M = (a, b, q = 1, t = 0, j = a.length) => {
  while (j--) + a[j] ? t += q : 0, q = (q * 10) % b;
  return t % b
}

// function D - Long division with arbitrary precision - a is a string of decimal digits
D = (a, b, r = '', z = 0) => [...a].map(a => (z += a, d = z / b | 0, z %= b, r || d ? r += d : 0)) && r

// Testable function 
f = n => {
  for (i = 0; ++i < 1e7 && (z = M(v = i.toString(2), n)););
  return z ? ['big'] : [D(v, n), v]
}

Ho funzionato su Firefox, ma non sembra gestire numeri più grandi, ad esempio 999
aditsu,

Ho una nuova versione in grado di gestire 999 in 36 secondi, ma non c'è speranza di raggiungere 9999 con il timeout javascript (ogni '9' aggiunto richiede 2 ^ 9 (~ 500) volte il tempo per terminare)
edc65

@aditsu è il meglio che posso fare in JavaScript (ma in C # è abbastanza lo stesso). Aspettando sempre una spiegazione del tuo incredibile algoritmo
edc65,

Ho aggiunto una spiegazione ora
aditsu,



4

PHP, 50 byte

while(preg_match('/[^01]/',$argv[1]*++$y));echo$y;

Alcuni casi di test

1 > 1
2 > 5
12 > 925
21 > 481

1
Stavo per fare qualcosa del genere, è anche un po 'più breve di quanto
pensassi

4

CJam, 19 17 16 bytes

li:V!{)_V*sAs-}g

Try it online

Brute force solution, trying values sequentially until one meeting the condition is found.

The latest version saves 2 bytes thanks to using As instead of "01" to build a string containing 0 and 1, as suggested by @aditsu. The full proposed solution in the comment saves another byte, but it looks fairly different from mine, so I didn't want to post it under my name.

And 1 more byte saved by @Dennis.

Explanation:

li      Get input and convert to int.
:V      Save it in variable V.
!       Negate the value. Since we saved it in V, we don't need it on the stack anymore.
        But we need 0 on the stack as the start value for y. This conveniently
        accomplishes both with a single operator, since the input is guaranteed to be
        larger than 0.
{       Loop over y.
  )       Increment y.
  _       Copy it.
  V*      Multiply with input in variable V.
  s       Convert to string.
  As      Push the string "10", as the number 10 converted to a string .
  -       Remove 0 and 1 digits. This will result in an empty list if there were only
          0 and 1 digits. The empty list is falsy, and will terminate the loop.
}g      End loop.

3
16: li0{1$+_sAs-}g\/
aditsu

Thanks, @aditsu. I didn't really want to copy your full solution under my name. I did take the As to build the string, since it's a very local change, which in hindsight (which is always much easier...) I should have thought of.
Reto Koradi

1
@RetoKoradi 16 bytes, less modifications: li:V!{)_V*sAs-}g Also, 0{)_easi*sAs-}g (15 bytes) works with the Java interpreter and command-line arguments.
Dennis

4

Python 3 2, 101 76 Bytes

-25 bytes thanks to @aditsu

almost as efficient as @aditsu's solution

99 -> 0.436 Seconds
72 -> 0.007 Seconds
b,m,n=1,1,input()
while b%n:
 b=int("{0:b}".format(m))
 m+=1
print b/n

Instead of trying to loop through the multiples in increasing order, I'm trying to loop through the products, which I'm generating in 'binary' form.


Not bad :) What about 9999?
aditsu

2
Some golfing tips: use python 2 (n=input()), while b%n: (initialize b to 1), no indentation
aditsu

@aditsu Thanks! 9999 hmmm, looks like it'll take a couple of days, well back to the drawing board -_-
Rnet

1
bin(m)[2:] should be shorter than the format string. Double assignment on b=m=1 should save a few as well.
primo

4

Java, 213 bytes

import java.math.*;class P{public static void main(String[]a){BigInteger b=new java.util.Scanner(System.in).nextBigInteger(),c,d=c=b.ONE;while(!(b.multiply(c)+"").matches("[01]+"))c=c.add(d);System.out.print(c);}}

Uses BigIntegers and as such has (for all reasonable intents and purposes) unbounded input size. Not sure about the complexity though, that depends on the growth rate of our function here.

Thanks to geobits and ypnypn for saving a handful of bytes.


Hi, how would you call this in your main method please? Trying it but not succeeding
Yassin Hajaj

You'd have to add the static modifier to the method.
SuperJedi224

1
The question says that the solution should be a complete program, not just a function.
raznagul

You can cut 15 with b.ONE and !(b.multiply(c)+"") (instead of toString()).
Geobits

@raznagul: Fixed.
SuperJedi224

4

C, 3675 bytes

So long for Code Golf...

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>

#define min_n 1
#define max_n 10000

unsigned *mod_list; // list of mods to check
unsigned mod_list_length; // number of mods to check
char *graph; // for each mod, the power of 10 that gives it

void BuildGraph(unsigned n)
{
    unsigned mod10 = 10 % n;
    int pow = 1;

    memset(graph, 0, n);
    if (n == 1)
        return;
    mod_list[0] = 0; // mod 0 - no path coming to it yet
    mod_list[1] = 1; // mod 1 - 10^0 coming to it
    mod_list_length = 2;
    while (graph[0] == 0)
    {
        // We are going to change mod_list_length by adding new nodes.
        // This should not affect the set of nodes we check, so save its old value.
        unsigned mod_list_limit = mod_list_length;
        for (unsigned i = 0; i < mod_list_limit; ++i)
        {
            unsigned mod = mod_list[i] + mod10;
            if (mod >= n)
                mod -= n;
            if (graph[mod] == 0 && mod != 1) // new node?
            {
                graph[mod] = pow; // record the power of 10 with which we come to this node
                mod_list[mod_list_length++] = mod; // add it to the list of nodes
                if (mod == 0) // found the path to 0?
                    return; // stop calculating
            }
        }
        mod10 = (unsigned long long)mod10 * 10 % n; // go to next power of 10
        ++pow;
    }
}

void PrintPath(unsigned n, char *out)
{
    // Going to output powers of 10 in descending order
    unsigned mod = 0; // start at node 0
    int prev_pow = graph[mod] + 1; // happens to be an acceptable initialization
    do {
        int pow = graph[mod];
        while (--prev_pow > pow) // output the proper number of 0-digits
            *out++ = '0';
        *out++ = '1'; // output the digit 1, corresponding to current power of 10
        if (pow == 0)
            break;
        unsigned mod10 = 1;
        for (int p = 0; p < pow; ++p)
            mod10 = (unsigned long long)mod10 * 10 % n;
        mod = (mod + n - mod10 % n) % n; // go to the preceding node
    } while (mod != 0);
    while (--prev_pow >= 0) // output the proper number of 0-digits
        *out++ = '0';
    *out++ = 0;
}

// The long division algorithm
void DivideAndPrint(char *product, unsigned n, FILE* file)
{
    unsigned long long temp = 0;
    int print = 0;
    while (*product != '\0')
    {
        temp = temp * 10 + *product++ - '0';
        if (temp >= n)
            print = 1;
        if (print)
        {
            unsigned quotient = (unsigned)(temp / n);
            unsigned remainder = temp % n;
            fputc('0' + quotient, file);
            temp = remainder;
        }
    }
    fputc('\n', file);
    assert(temp == 0); // if not divisible, there is a bug somewhere
}

void Calc(unsigned n, FILE* file)
{
    char result[99];
    BuildGraph(n);
    PrintPath(n, result);
    DivideAndPrint(result, n, file);
}

int main(int argc, char* argv[])
{
    unsigned n;

    if (argv[1])
    {
        FILE* file = fopen(argv[1], "wt");
        mod_list = calloc(max_n, sizeof(int));
        graph = calloc(max_n, 1);
        clock_t before = clock();
        for (n = min_n; n <= max_n; ++n)
        {
            Calc(n, file);
        }
        clock_t after = clock();
        fprintf(stderr, "Time: %f\n", (after - before) / (double)CLOCKS_PER_SEC);
    }
    else
    {
        scanf("%u", &n);
        mod_list = calloc(n, sizeof(int));
        graph = calloc(n, 1);
        Calc(n, stdout);
    }
}

Run with no command line parameters - it gets n from stdin and outputs the result to stdout. Run with a file name - it writes the results for n = 1...10000 into that file, and measures time.

Performance for 1...10000: 140 ms

This code uses the algorithm proposed by aditsu, implemented in C for speed. I made no effort to golf it, so the code would be easier to read.

I implemented it first in C++ using std::map to record the results of the search, and it was rather slow. However, the keys of the map are consecutive integers (I call them mods, because they represent numbers modulo n), so it's natural to use an array - so I rewrote it in C.

An additional optimization concerns the values of the mapping - in order to avoid storing a big integer for each mod, I store only the largest power of 10 there - it's just enough information to go to the previous mod. So the array is really a search tree/graph. When the search arrives to mod = 0, tracing the nodes of the tree back to the root gives the powers of 10 in descending order.

Since search usually stops rather quickly, with only a small fraction of nodes visited, I need a list of active nodes. It's implemented as an array mod_list with length mod_list_length.

Some runtime statistics (on a machine with 16 GB RAM, which seems to be important for large n, because the program allocates 5n bytes of memory):

  • Input 99999999 - 2 seconds
  • Input 999999999 - 27 seconds (the result is 111111111222222222333333333444444444555555555666666666777777777888888889 - probably the largest result possible for 32-bit integers)
  • Input 2147483647 - 26 seconds (the result is 4661316525084584315813)
  • Input 1999999998 - 52 seconds (probably the longest run time possible for 32-bit integers)

2
I understand that you're after the bounty, but even so this is a code-golf question, and the site rules require you to make some effort to golf your code.
Peter Taylor

Your program has 3546 bytes.
aditsu

@aditsu I measured the byte count in Windows, which uses the CR/LF style
anatolyg

4

C++11, many bytes, very fast, wow (1.5 s on 1999999998, 0.2 s on 1…10000)

(Golfed Python version below.)

We start with a concept somewhat similar to aditsu’s solution, where we inductively build up a collection of modular remainders reachable in n steps. But instead of waiting until we find remainder 0, we check for two found remainders a and b such that a·10^n + b = 0. This meet-in-the-middle approach halves the depth of the search tree, so it’s much faster on large inputs and uses much less memory.

Some benchmarks:

$ echo 99999999 | \time ./decbin
1111111122222222333333334444444455555555666666667777777788888889
0.18user 0.01system 0:00.20elapsed 99%CPU (0avgtext+0avgdata 69360maxresident)k
0inputs+0outputs (0major+16276minor)pagefaults 0swaps
$ echo 999999999 | \time ./decbin
111111111222222222333333333444444444555555555666666666777777777888888889
1.22user 0.04system 0:01.27elapsed 100%CPU (0avgtext+0avgdata 434776maxresident)k
0inputs+0outputs (0major+37308minor)pagefaults 0swaps
$ echo 2147483647 | \time ./decbin
4661316525084584315813
0.00user 0.00system 0:00.01elapsed 72%CPU (0avgtext+0avgdata 5960maxresident)k
0inputs+0outputs (0major+1084minor)pagefaults 0swaps
$ echo 1999999998 | \time ./decbin
555555556111111111666666667222222222777777778333333333888888889444444445
1.42user 0.08system 0:01.50elapsed 100%CPU (0avgtext+0avgdata 544140maxresident)k
0inputs+0outputs (0major+38379minor)pagefaults 0swaps
$ \time ./decbin 10000.out
0.19user 0.00system 0:00.20elapsed 100%CPU (0avgtext+0avgdata 3324maxresident)k
0inputs+264outputs (0major+160minor)pagefaults 0swaps

Code:

#include <algorithm>
#include <boost/iterator/transform_iterator.hpp>
#include <fstream>
#include <list>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

using namespace boost;
using namespace std;

static inline bool cmp_first_partnered(pair<int, pair<int, int>> a,
                                       pair<int, pair<int, int>> b) {
  return a.first < b.first;
}
static inline bool eq_first_partnered(pair<int, pair<int, int>> a,
                                      pair<int, pair<int, int>> b) {
  return a.first == b.first;
}

static pair<int, int> retrace(int modulus, int place, pair<int, int> state,
                              list<vector<int>>::iterator i,
                              list<vector<int>>::iterator j, string &ret) {
  if (i == j)
    return state;
  state = retrace(modulus, (place * 10LL) % modulus, state, next(i), j, ret);
  int remainder = state.first;
  long long k = state.second * 10LL;
  if (!binary_search(i->cbegin(), i->cend(), remainder)) {
    remainder = ((long long)remainder + modulus - place) % modulus;
    k += 1;
  }
  int digit = k / modulus;
  if (digit != 0 || ret.size())
    ret += '0' + digit;
  return make_pair(remainder, k % modulus);
}

static void mult(int modulus, int x, int y,
                 vector<pair<int, pair<int, int>>>::iterator i,
                 vector<pair<int, pair<int, int>>>::iterator j) {
  if (y - x == 1) {
    for (auto k = i; k != j; k++)
      k->first = (k->first * 10LL) % modulus;
    return;
  }

  int z = (x + y) / 2;
  vector<pair<int, pair<int, int>>>::iterator k = lower_bound(
      i, j, make_pair(int(((long long)modulus * z + 9) / 10), make_pair(0, 0)));
  mult(modulus, x, z, i, k);
  mult(modulus, z, y, k, j);
  inplace_merge(i, k, j,
                [](pair<int, pair<int, int>> a, pair<int, pair<int, int>> b) {
                  return make_pair(a.first, a.second.second) <
                         make_pair(b.first, b.second.second);
                });
}

static string go(int modulus) {
  if (modulus == 1)
    return "1";

  int sequence = 1;
  list<vector<int>> v = {{0}};
  vector<pair<int, pair<int, int>>> partnered;
  int place = 1;
  while (true) {
    v.emplace_back(v.rbegin()->size() * 2);
    vector<int> &previous = *next(v.rbegin()), &current = *v.rbegin();

    auto offset = [modulus, place, sequence](int a) {
      return (a + (long long)place) % modulus;
    };
    auto old_mid =
        lower_bound(previous.cbegin(), previous.cend(), modulus - place),
         new_mid = lower_bound(previous.cbegin(), previous.cend(), place);
    current.resize(
        set_union(new_mid, previous.cend(),
                  make_transform_iterator(previous.cbegin(), offset),
                  make_transform_iterator(old_mid, offset),
                  set_union(previous.cbegin(), new_mid,
                            make_transform_iterator(old_mid, offset),
                            make_transform_iterator(previous.cend(), offset),
                            current.begin())) -
        current.begin());

    int place2 = modulus - (long long)place * place % modulus;
    auto offset_partnered = [modulus, place, place2,
                             sequence](pair<int, pair<int, int>> a) {
      return make_pair((a.first + (long long)place2) % modulus,
                       make_pair((a.second.first + (long long)place) % modulus,
                                 sequence + a.second.second));
    };
    auto old_mid_partnered =
        lower_bound(partnered.cbegin(), partnered.cend(),
                    make_pair(modulus - place2, make_pair(0, 0))),
         new_mid_partnered = lower_bound(partnered.cbegin(), partnered.cend(),
                                         make_pair(place2, make_pair(0, 0)));
    vector<pair<int, pair<int, int>>> next_partnered(partnered.size() * 2 + 1);
    auto i =
        set_union(partnered.cbegin(), new_mid_partnered,
                  make_transform_iterator(old_mid_partnered, offset_partnered),
                  make_transform_iterator(partnered.cend(), offset_partnered),
                  next_partnered.begin(), cmp_first_partnered);
    if (new_mid_partnered == partnered.cend() ||
        new_mid_partnered->first != place2)
      *i++ = make_pair(place2, make_pair(place, sequence));
    next_partnered.resize(
        set_union(new_mid_partnered, partnered.cend(),
                  make_transform_iterator(partnered.cbegin(), offset_partnered),
                  make_transform_iterator(old_mid_partnered, offset_partnered),
                  i, cmp_first_partnered) -
        next_partnered.begin());
    partnered.swap(next_partnered);

    sequence += previous.size();

    place = (place * 10LL) % modulus;

    mult(modulus, 0, 10, partnered.begin(), partnered.end());
    partnered.resize(
        unique(partnered.begin(), partnered.end(), eq_first_partnered) -
        partnered.begin());

    auto with_first = [](int a) { return make_pair(a, make_pair(a, 0)); };

    vector<pair<int, pair<int, int>>> hits;
    set_intersection(partnered.cbegin(), partnered.cend(),
                     make_transform_iterator(current.cbegin(), with_first),
                     make_transform_iterator(current.cend(), with_first),
                     back_inserter(hits), cmp_first_partnered);

    if (hits.size()) {
      pair<int, pair<int, int>> best = *min_element(
          hits.begin(), hits.end(),
          [](pair<int, pair<int, int>> a, pair<int, pair<int, int>> b) {
            return a.second.second < b.second.second;
          });
      string ret = "";
      pair<int, int> state =
          retrace(modulus, 1, make_pair(best.second.first, 0), v.begin(),
                  prev(v.end()), ret);
      retrace(modulus, 1, make_pair(best.first, state.second), v.begin(),
              prev(v.end()), ret);
      return ret;
    }
  }
}

int main(int argc, const char *argv[]) {
  ios_base::sync_with_stdio(false);
  if (argc >= 2) {
    ofstream ofs(argv[1]);
    for (int modulus = 1; modulus <= 10000; modulus++)
      ofs << go(modulus) << '\n';
  } else {
    int modulus;
    cin >> modulus;
    cout << go(modulus) << '\n';
  }
  return 0;
}

Python, 280 bytes (8.6 seconds on 1999999998 with PyPy)

n=input()
if n<2:print 1;exit()
d={0:0}
l=[]
k=1
b=x=y=0
while 1:
 for a in[0]+l:
  m=(a+k)%n
  if m not in d:l.append(m);d[m]=b
 k=(k*10)%n;b+=1
 for a in l:
  if(-k*a)%n in d:
   while(a-x)%n:x+=10**d[(a-x)%n]
   while(-y-k*a)%n:y+=10**d[(-y-k*a)%n]
   print(10**b*x+y)/n;exit()

2
I understand that you're after the bounty, but even so this is a code-golf question, and the site rules require you to make some effort to golf your code.
Peter Taylor

1
@PeterTaylor, very well, I added a golfed version in Python.
Anders Kaseorg

3

Mathematica 115 bytes

p=Drop[Union[FromDigits/@Flatten[Table[Tuples[{0,1},{k}],{k,2,12}],1]],2];
i=Input[];FirstCase[p,x_/;Divisible[x,i]]

3

Java 156 bytes

public class P{public static void main(String[]a){long x=Long.valueOf(a[0]),y;for(y=2;!(""+x*y).replaceAll("1|0","").isEmpty();y++);System.out.println(y);}}

Massive thanks to aditsu :)


You don't need a space after [], y can be long too, you forgot the x*y+"" trick in the 2nd program, use isEmpty instead of checking the length, use ; instead of {}
aditsu

Anyway, welcome to code golf :)
aditsu

I must say, I am impressed, but making y long wouldn't make the code shorter
Joba

Yes it would: long x=…,y;
aditsu

y must start from 1, you can initialize it in the declaration, your class doesn't need to be public, and you can move y++ to the x*y part (x*y++)
aditsu

2

Pyth - 12 11 bytes

Uses filter with numeric arg to get first natural number that fulfills predicate, default is 1 which is what we want. Setwise diff to check if only zeros and ones.

f!-j*QT10U2

Test Suite.


Convert to string and remove "01. Saves one char.
Jakube

2

R, 45 bytes

x=scan();y=2;while(grepl("[2-9]",x*y))y=y+1;y

Usage:

> x=scan();y=2;while(grepl("[2-9]",x*y))y=y+1;y
1: 2
2: 
Read 1 item
[1] 5
> x=scan();y=2;while(grepl("[2-9]",x*y))y=y+1;y
1: 21
2: 
Read 1 item
[1] 481
> x=scan();y=2;while(grepl("[2-9]",x*y))y=y+1;y
1: 42
2: 
Read 1 item
[1] 2405

2

Java, 198 193 181 bytes

Thanks to @aditsu for shaving off 5 bytes AND increasing the range of testable numbers!

Note that some values loop negatively due to how Java parses integers. This could be circumvented by BigInteger, but the bonus was simply less valuable.

I know that I'm not going to win, but I hope this inspires other, shorter, answers.

class A{public static void main(String[] a){for(long i=1;;i++){try{long b=Long.parseLong(a[0]);if(b*i<0)break;Long.parseLong(b*i+"",2);System.out.println(i);}catch(Exception e){}}}}

Ungofled:

class A {
   public static void main(String[] a){
      for(long i=1;;i++){ // infinite loop starting at 1
         try{ // if an error is thrown by attempting to parse as binary, restart while adding 1 to i
            long b=Long.parseLong(a[0]); // For later - it was shorter to declare than use twice
            if(b*i<0)break; // Break out of the program if we have looped.
            Long.parseLong(b*i+"",2); // Multiply out and see if it's passable as a binary number, otherwise, throw error and go back to the top of the loop
            System.out.println(b); // print it out
         } catch (Exception e) {} // do nothing on catch
      }
   }
}

2
It's funny that Long is shorter than Integer :)
anatolyg

3
The most literal irony there is.
Addison Crump

2

C, 107 101 bytes (105 99 bytes for 32-bits)

There is a distinct lack of answers in C on code golf. Indeed, C is not the best choice for writing the smallest possible program, but it's not that bad:

main(d,b){char s[9];gets(s);for(b=atoi(s);sprintf(s,"%d",b*d),strspn(s,"01")[s];d++);printf("%d",d);}

You can do without the #includes, but then all the function definitions will be implicit. The main drawback is that this causes the assumption that all functions return ints. This is a problem on 64-bit machines for functions that actually return a pointer. If you are on a 32-bit machine, 2 bytes can be shaved off the above solution:

main(d,b){char s[9];for(b=atoi(gets(s));sprintf(s,"%d",b*d),strspn(s,"01")[s];d++);printf("%d",d);}

Somewhat more readable version:

int main()
{
  char s[9];
  gets(s);
  int d = 1;
  int b = atoi(s);
  for (; sprintf(s, "%d", b * d), strspn(s, "01")[s]; d++);
  printf("%d", d);
}

2

C# time near 5 seconds (1 to 10000)

As requested, here is a golfed C# program answering the original challenge. Input as command line argument, output to console.

using System;using System.Collections.Generic;using System.Numerics;using System.Linq;
class P{static void Main(string[] a){int m,n=int.Parse(a[0]);var d=new Dictionary<int,long>();long b;int h;
for(d[n]=0,b=h=1;;b*=2,h=(h*10)%n)foreach(int k in d.Keys.Reverse())if(!d.ContainsKey(m=(h+k)%n)){
var w=d[k]|b;if(m==0){Console.Write(BigInteger.Parse(Convert.ToString(w,2))/n);return;}d.Add(m,w);}}}

Then, as for the bounty: the bounty should go to aditsu, as I think his algorithm cannot be beaten in terms of perfomance. But anatolyg self-answer is amazing too.

Here is my fast implementation in C#. I suppose that in C++ it could be faster (maybe 2x). Compiled and tested with Visual Studio 2010, .NET framework 4, 64 bits, redirecting output to nul. Time : 00:00:05.2604315

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Numerics;
using System.Diagnostics;

class Program
{
   static BigInteger Find(int n)
   {
      var d = new Dictionary<int, long>();
      long kb;
      int km;
      d[n] = 0;
      for (kb = km = 1; ; kb *= 2, km = (km * 10) % n)
      {
         foreach (int key in d.Keys.Reverse())
         {
            int m = (km + key) % n;
            if (!d.ContainsKey(m))
            {
               long w = d[key] | kb;
               if (m == 0)
               {
                  return BigInteger.Parse(Convert.ToString(w, 2));
               }
               d.Add(m, w);
            }
         }
      }
   }

   static void Exec(int n, out string sq, out string sa)
   {
      var v = Find(n);
      sq = (v/n).ToString();
      sa = v.ToString();
   }  

   static void Main(string[] args)
   {
      // string n = Console.ReadLine();
      int limit = int.Parse(args[0]);
      string q ="", a = "";
      Stopwatch x = new Stopwatch();
      x.Start();
      for (int n = 1; n <= limit; n++)
      {
         Exec(n, out q, out a);
         Console.WriteLine("{0} {1} {2}", n, q, a);
      }
      x.Stop();
      Console.Error.WriteLine("{0}", x.Elapsed);
   }
}

Times 4.1s. I misspoke in the bounty. With the latest version of PyPy, aditsu's faster version times approximately 8s, so this is twice as fast.
primo

I understand that you're after the bounty, but even so this is a code-golf question, and the site rules require you to make some effort to golf your code.
Peter Taylor

I'm not after the bounty, it's just an example of implementation. But you're right, I'll add a golfed version.
edc65

@PeterTaylor could it go now?
edc65

By the way, why Keys.Reverse? Is the order important? If it's just to avoid concurrency issues, ToList is shorter.
Peter Taylor

2

C with GMP (621 bytes, fast)

I've tried to be fast and short, but favoured fast. This implementation uses a slightly improved version of the number-theoretic speedup I mentioned in a comment on aditsu's answer.

Save as pseudobinary.c and compile with gcc pseudobinary.c -lgmp -o pseudobinary. Note that this allocates so much memory for large inputs that you will need to compile it for a 64-bit platform.

#include <gmp.h>
int main(int y,char*z[]){int i,n,b,c,e,f,m,*j,*k,*l,*r,*h;char *d,*s;mpz_t
B,I,Q;i=atoi(z[1]);n=i;for(b=0;n%10<1;++b)n/=10;for(;n%2<1;++b)n/=2;for(;n%5<1;++b)n/=5;if(n<2)--b;d=calloc(n,1);j=calloc(n,sizeof(int));r=calloc(99,sizeof(int));c=2;d[1]=1;*j=r[1]=e=1;l=j+1;for(s=0;!s;++c){r[c]=e=e*10%n;k=l;for(h=j;h<k;h++){f=*h;m=(e+f)%n;if(d[m]<1){*l++=m;if(m<1){s=malloc(99);memset(s,48,99);for(f=c;f;f=d[m=(m+n-r[f])%n])s[c-f]++;s[c]=0;h=k;}d[m]=c;}}}f=strlen(s);s[f]=48;s[f+b]=0;mpz_init_set_str(B,s,10);mpz_init_set_si(I,i);mpz_init(Q);mpz_divexact(Q,B,I);d=mpz_get_str(0,10,Q);printf("%s\n",d);return 0;}

Loop version for timing (751 bytes)

#include <gmp.h>
char **v;int main(){int i,n,b,c,e,f,m,*j,*k,*l,*r,*h;char *d,*s;mpz_t
B,I,Q;v=calloc(10001,sizeof(char*));v[1]=s=malloc(99);memset(s,48,99);*s=49;s[1]=0;for(i=0;++i<10001;){n=i;for(b=0;n%10<1;++b)n/=10;for(;n%2<1;++b)n/=2;for(;n%5<1;++b)n/=5;d=calloc(n,1);j=calloc(n,sizeof(int));r=calloc(99,sizeof(int));c=2;d[1]=1;*j=r[1]=e=1;l=j+1;for(;!v[n];++c){r[c]=e=e*10%n;k=l;for(h=j;h<k;h++){f=*h;m=(e+f)%n;if(d[m]<1){*l++=m;if(m<1){v[n]=s=malloc(99);memset(s,48,99);for(f=c;f;f=d[m=(m+n-r[f])%n])s[c-f]++;s[c]=0;h=k;}d[m]=c;}}}free(d);free(j);free(r);s=v[n];f=strlen(s);s[f]=48;s[f+b]=0;mpz_init_set_str(B,s,10);mpz_init_set_si(I,i);mpz_init(Q);mpz_divexact(Q,B,I);d=mpz_get_str(0,10,Q);printf("%s\n",d);free(d);s[f+b]=48;s[f]=0;}return 0;}

Ungolfed loop version

#include <gmp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char **cache;

int main() {
    int i,n,shift,_kb,km,key,m,*ks,*ksi,*nksi,*res,*ii;
    char *d,*s;
    mpz_t B,I,Q;

    cache = calloc(10001,sizeof(char*));
    if (!cache) { printf("Failed to malloc cache\n"); return 1; }
    cache[1]=s = malloc(99);
    memset(s,48,99);
    *s=49;
    s[1]=0;
    for (i=0;++i<10001;) {
        n=i;
        for(shift=0;n%10<1;++shift)n/=10;
        for(;n%2<1;++shift)n/=2;
        for(;n%5<1;++shift)n/=5;

        d = calloc(n,1);
        if (!d) { printf("Failed to malloc d\n"); return 1; }

        ks = calloc(n,sizeof(int));
        if (!ks) { printf("Failed to malloc ks\n"); return 1; }

        res = calloc(99,sizeof(int));
        if (!res) { printf("Failed to malloc res\n"); return 1; }

        _kb = 2;
        d[1] = 1;
        *ks = res[1] = km = 1;
        nksi = ks + 1;

        for(;!cache[n];++_kb) {
            res[_kb] = km = km*10%n;
            ksi = nksi;
            for (ii = ks; ii < ksi; ii++) {
                key = *ii;
                m = (km + key) % n;
                if (d[m] < 1) {
                    *nksi++ = m;
                    if (m < 1) {
                        cache[n] = s = malloc(99);
                        if (!s) { printf("Failed to malloc s\n"); return 1; }
                        memset(s,48,99);
                        for(key=_kb;key;key = d[m = (m + n - res[key]) % n])s[_kb-key]++;
                        s[_kb]=0;
                        ii = ksi; // break
                    }
                    d[m] = _kb;
                }
            }
        }

        free(d);
        free(ks);
        free(res);

        // Add shift * '0'
        s=cache[n];
        key=strlen(s);
        s[key]=48;
        s[key+shift]=0;

        // convert to big integer, divide, print
        mpz_init_set_str(B,s,10);
        mpz_init_set_si(I,i);
        mpz_init(Q);
        mpz_divexact(Q,B,I);
        d = mpz_get_str(0,10,Q);
        if (!s) { printf("Failed to malloc quotient\n"); return 1; }
        printf("%s\n", d);
        free(d);

        // Remove shift * '0'
        s[key+shift]=48;
        s[key]=0;
    }
    return 0;
}

2

C + GMP, 669

This is really fast for smallish numbers; it starts to choke when the result has more than 64 digits.

#include<gmp.h>
#define B(x)(int)((x*(long)k)%n);
int*M,*H,P[99],n,x,p,q=2,e=1,k=10,y,f,z;char*E,C[99];int b(int k,int t){int
j=E[k],a=1<<(j-2);if(j<2){C[t]=49;return 1;}x=(int)((k+n-P[j]*(long)H[k]%n)%n);if(x)b(x,t);return a+b(H[k],t-a);}int
main(){scanf("%d",&n);E=calloc(n+1,1);M=calloc(n+1,4);H=malloc(n*4);M[1]=E[1%n]=P[1]=1;while(!E[0]){P[++e]=k;p=q;for(x=0;++x<p;){y=B(M[x])if(E[n-y]){E[0]=e;H[0]=M[x];break;}}if(!E[x=0])while(++x<p){y=B(M[x])for(z=0;z<p;++z){f=y+M[z];if(f>=n)f-=n;if(!E[f]){E[f]=e;H[f]=M[x];M[q++]=f;}}}k=B(k)}memset(C,48,98);C[99]=0;x=b(0,97);mpz_t
m,r;mpz_init(r);mpz_init_set_str(m,C+98-x,10);mpz_fdiv_q_ui(r,m,n);puts(mpz_get_str(C,10,r));}

Version that loops to 10000 (671 bytes):

#include<gmp.h>
#define B(x)(int)((x*(long)k)%n);
#define N 10001
int M[N],H[N],P[99],n=0,x,p,q,e,k,y,f,z;char E[N],C[99];int b(int k,int t){int
j=E[k],a=1<<(j-2);if(j<2){C[t]=49;return 1;}x=(int)((k+n-P[j]*(long)H[k]%n)%n);if(x)b(x,t);return a+b(H[k],t-a);}int
main(){while(++n<N){memset(E,M[0]=0,n);M[1]=E[1%n]=P[1]=e=1;q=2;k=10;while(!E[0]){P[++e]=k;p=q;for(x=0;++x<p;){y=B(M[x])if(E[n-y]){E[0]=e;H[0]=M[x];break;}}if(!E[x=0])while(++x<p){y=B(M[x])for(z=0;z<p;++z){f=y+M[z];if(f>=n)f-=n;if(!E[f]){E[f]=e;H[f]=M[x];M[q++]=f;}}}k=B(k)}memset(C,48,98);C[99]=0;x=b(0,97);mpz_t
m,r;mpz_init(r);mpz_init_set_str(m,C+98-x,10);mpz_fdiv_q_ui(r,m,n);puts(mpz_get_str(C,10,r));}}

Here are some commands for testing my code as well as my competitors', and the results on my laptop:

ls -l *.c*       
-rw-r--r-- 1 aditsu aditsu  669 Oct 27 15:01 mult-aditsu-single.c
-rw-r--r-- 1 aditsu aditsu  671 Oct 27 15:01 mult-aditsu.c
-rw-r--r-- 1 aditsu aditsu 3546 Oct 27 15:01 mult-anatoly.c
-rw-r--r-- 1 aditsu aditsu 6175 Oct 27 15:01 mult-anders.cpp
-rw-r--r-- 1 aditsu aditsu  621 Oct 27 15:01 mult-peter-single.c
-rw-r--r-- 1 aditsu aditsu  751 Oct 27 15:01 mult-peter.c

gcc -w -march=native -O3 mult-aditsu-single.c -lgmp -o mult-aditsu-single
gcc -w -march=native -O3 mult-aditsu.c -lgmp -o mult-aditsu
gcc -w -march=native -O3 mult-peter-single.c -lgmp -o mult-peter-single
gcc -w -march=native -O3 mult-peter.c -lgmp -o mult-peter
gcc -w -march=native -O3 --std=c99 mult-anatoly.c -o mult-anatoly
g++ --std=c++11 -march=native -O3 mult-anders.cpp -o mult-anders

for i in {1..5}; do time ./mult-anders mult-anders.txt; done
./mult-anders mult-anders.txt  0.34s user 0.00s system 99% cpu 0.344 total
./mult-anders mult-anders.txt  0.36s user 0.00s system 99% cpu 0.358 total
./mult-anders mult-anders.txt  0.34s user 0.00s system 99% cpu 0.346 total
./mult-anders mult-anders.txt  0.35s user 0.00s system 99% cpu 0.347 total
./mult-anders mult-anders.txt  0.34s user 0.00s system 99% cpu 0.344 total

for i in {1..5}; do ./mult-anatoly mult-anatoly.txt; done
Time: 0.254416
Time: 0.253555
Time: 0.245734
Time: 0.243129
Time: 0.243345

for i in {1..5}; do time ./mult-peter > mult-peter.txt; done
./mult-peter > mult-peter.txt  0.14s user 0.00s system 99% cpu 0.137 total
./mult-peter > mult-peter.txt  0.15s user 0.00s system 97% cpu 0.153 total
./mult-peter > mult-peter.txt  0.15s user 0.00s system 99% cpu 0.149 total
./mult-peter > mult-peter.txt  0.15s user 0.00s system 99% cpu 0.150 total
./mult-peter > mult-peter.txt  0.14s user 0.00s system 99% cpu 0.138 total

for i in {1..5}; do time ./mult-aditsu > mult-aditsu.txt; done
./mult-aditsu > mult-aditsu.txt  0.06s user 0.00s system 95% cpu 0.058 total
./mult-aditsu > mult-aditsu.txt  0.05s user 0.00s system 97% cpu 0.055 total
./mult-aditsu > mult-aditsu.txt  0.06s user 0.00s system 99% cpu 0.056 total
./mult-aditsu > mult-aditsu.txt  0.05s user 0.00s system 99% cpu 0.054 total
./mult-aditsu > mult-aditsu.txt  0.05s user 0.00s system 98% cpu 0.055 total

md5sum *.txt
6eef8511d3bc5769b5d9218be2e00028  mult-aditsu.txt
6eef8511d3bc5769b5d9218be2e00028  mult-anatoly.txt
6eef8511d3bc5769b5d9218be2e00028  mult-anders.txt
6eef8511d3bc5769b5d9218be2e00028  mult-peter.txt

An answer well-deserving of a bounty. I take particular interest in this problem (and your initial solution), because it is a special case of the subset sum problem, which is known to be NP-complete (given a list of the residues of 10ⁱ mod n, find the earliest subset which sums to n).
primo

@primo Thank you :) My approach here is different - I double the number of digits at each step rather than just incrementing it, and I also check first (very quickly) if any of the new numbers would be a solution, before actually calculating them. And I'm sure there's still room for golfing.
aditsu

Interesting. When I tried doubling the number of digits at each step it ended up being slower. Maybe the pre-check for solutions makes a big difference.
Peter Taylor

@PeterTaylor that's possible.. it seems that you're also calling calloc in a loop, which might slow it down. Anyway, I'd like to add an ungolfed version of my code when I find some time, and I also have an idea how to make it faster for bigger/nastier numbers.
aditsu

2

T-SQL, 164 156 155 154 159 bytes

(-1 byte. Thanks Jonathan!)

(-1 more because why do I have trailing spaces on lines? SMH)

(+5 realized my golfing broke things)

create function b(@ int)
returns int
as begin
declare @b varchar(max)='',@i int=@
while @>0SELECT @b=cast(@%2 as varchar)+@b,@/=2
return cast(@b as int)/@i
end

I don't know why I keep coming back to these questions where I'm supposed to convert to Binary... T-SQL doesn't know how to do that right.

In any case, here's a SQLFiddle.

Un-golfed:

create function binarySquare(@id int)
returns int 
as BEGIN

Most of this stuff is required to write a function in T-SQL, as far as I'm aware.

    declare @bin nvarchar(max) = ''

Create a blank string that we're going to store as our binary number.

    declare @id2 int = @id

Save the input value for use at the end. It seems like there should be a way to use the original input even if we change the value, but I can't find one.

    while @id>0
      BEGIN
        SET @bin = cast(@id%2 as varchar(1)) + @bin

So we take our original input, MOD it with 2 to find the remainder, and that's going to be our next smallest digit. For example, 5%2 = 1

        SET @id = @id/2

Then we take our number, and divide it in half. Because it's an int type, it rounds it down to the nearest whole number, so 5/2 = 2. END We then loop through this until the value is 0. So we end up with 5%2 = 1 5/2 = 2 2%2 = 0 2/2 = 1 1%2 = 1 1/2 = 0 which gives us our binary string value of 101.

    declare @binNum int = (SELECT cast(@bin as int))

We take our binary string and convert it back to an int again.

    return @binNum/@id2

We return our binary string int divided by our original value, per the origin of the question.

END

Is the space in @>0 SELECT not omittable?
Jonathan Frech

Nice catch! I can never remember what spaces are omit-able...
phroureo

Most of the time you can omit spaces in between literals and variables / keywords, as they cannot begin with a digit.
Jonathan Frech

1

Ruby, 46 bytes

I should really eliminate the while loop with an alternative loop.

n,k=gets,0;$_="#{n.to_i*k+=1}"while/[^01]/;p k

Edit: Thanks @manatwork for shaving off 1 byte!

Edit2: Thanks @histocraft for the insane 9 bytes!

Edit: Thanks @manatwork again for shaving off 7 bytes!


z!=z[/[01]+/] is shorter. z[/[^01]/] is even more shorter.
manatwork

@manatwork Thanks! 1 byte less!
Peter Lenkefi

2
Single-line while loops tend to be the shortest: z="#{n.to_i*k+=1}"while z[/[^01]/]
histocrat

@histocrat That's 9 bytes! And I didn't even know that ruby is capable of this. Thanks!
Peter Lenkefi

Interesting that you not changed the test to negated character set neither after was suggested 2nd time. Any reason?
manatwork

1

Scala, 114 Bytes

val i=scala.io.StdIn.readInt;Stream.from(1).foreach{x=>if((i*x+"").map{_.asDigit}.max<2){print(x);System.exit(0)}}

Readable version

val i=scala.io.StdIn.readInt
Stream.from(1).foreach{x => 
    if((i*x+"").map{_.asDigit}.max<2) {
        print(x)
        System.exit(0)
    }
}

1

gawk4 brute force, 28+2 = 30 bytes

{while(++n*$0~/[2-9]/);}$0=n

Needs to be called with the -M option for using big numbers. Of course this is ridiculously slow, using big numbers slows it down even more, but theoretically the input is not limited, and RAM usage is negligible.

Usage example ( if you got time to waste ;))

echo 27 | awk -M '{while(++n*$0~/[2-9]/);}$0=n'

gawk4 optimized, 69+2 = 71 bytes

{for(;!a[0];NR*=10)for(i in a)a[j=(s=a[i]+NR)%$0]?0:a[j]=s}$0=a[0]/$0

Well, this ended up being a clone of aditsu's answer. After looking at this question I was still figuring out how to code the subset-sum part, when I couldn't resist looking at the other answers here.

In awk array elements have the (strange ?) behaviour that if you compare a non-existing element to something it is somehow initialized as empty before being compared (I'll admit that I'm not quite sure about what is happening there). So after checking !a[0] the for(i in a) loop starts even without initializing a[$0] to 0 as aditsu did.

Of course the -M option has to be used for this too.

Though it is rather fast it is still remarkably slower than Python. For 79992 this takes around 14 seconds on my 2GHz Core2Duo. And I wouldn't say it works for inputs up to 2^31, because in the worst case it has to build an array of big numbers (gawk4 uses GMP for this), which has the size of the input number. As a 'bonus' large arrays are very very slow in awk...


1

Dyalog APL, 25

This defines a proper program "P" (not just an unnamed function):

P←2∘{0::⍵∇⍨1+⍺⋄⍺⊣~⍎¨⍕⍺×⍵}

2∘ begin with 2 as left argument
0:: if there is any error...
⍵∇⍨1+⍺ call itself with an incremented left argument
⍺×⍵ multiply left and right arguments
make into string
⍎¨ make each character into a number
~ attempt logical NOT (if it fails, go to error-handling above, else...)
⍺⊣ return the current left argument.

      P 2
50
      P 21
481
      P¨⍳8    ⍝ 1 through 8
10 5 37 25 2 185 143 125
Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.