n * k = dd0d00d dove d =…?


14

Dato un numero intero positivo n ≤ 500 :

  • Trova il numero intero positivo più piccolo k in modo tale che tutte le cifre nella rappresentazione decimale di n * k siano 0 o d , con 1 ≤ d ≤ 9 .

  • Stampa o ritorna d in meno di 30 secondi (leggi di più nella sezione Chiarimenti e regole ).

Semplici esempi

Ecco i primi 30 valori di d .

+----+-------+---------+---+    +----+-------+---------+---+
|  n |     k |   n * k | d |    |  n |     k |   n * k | d |
+----+-------+---------+---+    +----+-------+---------+---+
|  1 |     1 |       1 | 1 |    | 16 |     5 |      80 | 8 |
|  2 |     1 |       2 | 2 |    | 17 |   653 |   11101 | 1 |
|  3 |     1 |       3 | 3 |    | 18 |     5 |      90 | 9 |
|  4 |     1 |       4 | 4 |    | 19 |   579 |   11001 | 1 |
|  5 |     1 |       5 | 5 |    | 20 |     1 |      20 | 2 |
|  6 |     1 |       6 | 6 |    | 21 |    37 |     777 | 7 |
|  7 |     1 |       7 | 7 |    | 22 |     1 |      22 | 2 |
|  8 |     1 |       8 | 8 |    | 23 |  4787 |  110101 | 1 |
|  9 |     1 |       9 | 9 |    | 24 |    25 |     600 | 6 |
| 10 |     1 |      10 | 1 |    | 25 |     2 |      50 | 5 |
| 11 |     1 |      11 | 1 |    | 26 |    77 |    2002 | 2 |
| 12 |     5 |      60 | 6 |    | 27 |    37 |     999 | 9 |
| 13 |    77 |    1001 | 1 |    | 28 |    25 |     700 | 7 |
| 14 |     5 |      70 | 7 |    | 29 | 37969 | 1101101 | 1 |
| 15 |     2 |      30 | 3 |    | 30 |     1 |      30 | 3 |
+----+-------+---------+---+    +----+-------+---------+---+

Esempi non così facili

Una particolarità di questa sfida è che alcuni valori sono molto più difficili da trovare rispetto ad altri - almeno con un approccio puramente bruto. Di seguito sono riportati alcuni esempi di n che portano a un valore elevato di k .

+-----+------------+---------------+---+    +-----+------------+---------------+---+
|   n |          k |         n * k | d |    |   n |          k |         n * k | d |
+-----+------------+---------------+---+    +-----+------------+---------------+---+
|  81 |   12345679 |     999999999 | 9 |    | 324 |   13717421 |    4444444404 | 4 |
| 157 |   64338223 |   10101101011 | 1 |    | 353 |   28615017 |   10101101001 | 1 |
| 162 |   13717421 |    2222222202 | 2 |    | 391 |  281613811 |  110111000101 | 1 |
| 229 |   43668559 |   10000100011 | 1 |    | 405 |   13717421 |    5555555505 | 5 |
| 243 |   13717421 |    3333333303 | 3 |    | 439 |   22781549 |   10001100011 | 1 |
| 283 |   35371417 |   10010111011 | 1 |    | 458 |   43668559 |   20000200022 | 2 |
| 299 |   33478599 |   10010101101 | 1 |    | 471 |   64338223 |   30303303033 | 3 |
| 307 |   32576873 |   10001100011 | 1 |    | 486 |   13717421 |    6666666606 | 6 |
| 314 |   64338223 |   20202202022 | 2 |    | 491 |  203871711 |  100101010101 | 1 |
| 317 | 3154574483 | 1000000111111 | 1 |    | 499 |   22244489 |   11100000011 | 1 |
+-----+------------+---------------+---+    +-----+------------+---------------+---+

Chiarimenti e regole

  • n * k conterrà sempre almeno una cifra d , ma potrebbe non contenere affatto zero.
  • Questo è , quindi vince il codice più breve in byte. Tuttavia, il programma o la funzione deve essere in grado di restituire il risultato per 1 ≤ n ≤ 500 in meno di 30 secondi su hardware di fascia media.
  • Tieni presente che alcuni valori sono più difficili da trovare rispetto ad altri. È improbabile che un programma che provi a forzare la forza del valore di k rispetti il ​​vincolo del limite di tempo (un buon caso di test è n = 317 ). Esistono metodi significativamente più veloci per trovare d .

Tabella di riferimento

Tutti i valori di d per 1 ≤ n ≤ 500 sono elencati di seguito.

n       | d
--------+--------------------------------------------------
001-025 | 1 2 3 4 5 6 7 8 9 1 1 6 1 7 3 8 1 9 1 2 7 2 1 6 5
026-050 | 2 9 7 1 3 1 8 3 2 7 9 1 2 3 4 1 6 1 4 9 2 1 6 7 5
051-075 | 3 4 1 9 5 7 1 2 1 6 1 2 9 8 5 6 1 4 3 7 1 9 1 2 3
076-100 | 4 7 6 1 8 9 2 1 4 5 2 3 8 1 9 1 4 3 2 5 6 1 7 9 1
101-125 | 1 6 1 8 7 2 1 9 1 1 1 7 1 2 5 4 9 2 7 6 1 2 3 4 5
126-150 | 6 1 8 3 1 1 6 7 2 9 8 1 6 1 7 1 2 1 9 5 2 7 4 1 3
151-175 | 1 8 9 7 5 4 1 2 1 8 7 2 1 4 3 2 1 8 1 1 3 4 1 6 7
176-200 | 8 3 2 1 9 1 2 1 8 5 6 1 4 9 1 1 6 1 2 3 7 1 9 1 2
201-225 | 3 2 7 4 5 2 9 8 1 7 1 4 1 2 5 9 7 2 3 2 1 2 1 7 9
226-250 | 2 1 4 1 1 3 8 1 6 5 4 3 7 1 6 1 2 3 4 7 6 1 8 3 5
251-275 | 1 6 1 2 3 8 1 6 7 2 9 2 1 6 5 7 3 4 1 9 1 8 3 2 5
276-300 | 6 1 2 9 7 1 2 1 4 5 2 7 9 1 1 3 4 1 7 5 8 9 2 1 3
301-325 | 7 2 3 8 5 6 1 4 3 1 1 8 1 2 9 4 1 2 1 8 1 7 1 4 5
326-350 | 2 1 8 7 3 1 4 3 2 5 8 1 2 3 2 1 6 1 8 3 2 1 4 1 7
351-375 | 9 8 1 6 5 4 7 2 1 9 1 2 3 4 5 2 1 8 9 1 7 6 1 2 3
376-400 | 8 1 9 1 2 3 2 1 6 7 2 9 4 1 3 1 7 1 2 5 9 1 2 7 4
401-425 | 1 6 1 4 5 2 1 8 1 1 3 4 7 9 5 8 1 2 1 6 1 2 3 8 5
426-450 | 2 7 4 3 1 1 9 1 7 3 4 1 6 1 4 3 2 1 4 5 2 3 7 1 9
451-475 | 1 4 3 2 5 8 1 2 9 2 1 6 1 8 3 2 1 6 7 1 3 8 1 6 5
476-500 | 7 3 2 1 6 1 2 3 4 5 6 1 8 3 7 1 6 1 2 9 8 7 6 1 5

1
Liberamente ispirato (ma abbastanza diverso da) da questa recente sfida .
Arnauld,

n = 6669666 -> d = 9
J42161217

Diagonali interessanti in quella tabella.
James,

@James Indeed. Gli schemi sembrerebbero un po 'più chiari formattando MOD 24. Con MOD 25 otteniamo invece delle diagonali. :-)
Arnauld l'

Risposte:


3

Gelatina , 16 15 14 byte

²B€Ḍ9×þF%Þ¹ḢQS

Runtime quadratico (meno di 25 secondi su TIO).

Provalo online!

Versione alternativa, 15 byte

2ȷB€Ḍ9×þF%Þ¹ḢQS

Runtime costante (circa 1 secondo su TIO).

Provalo online!

Come funziona

²B€Ḍ9×þF%Þ¹ḢQS  Main link. Argument: n

²               Take the square of n.
                This bound is high enough for all integers up to 500. 
                In fact, The highest value we need is 1387 for input 471, so
                2000 (2ȷ) is also enough (and a lot faster).

 B€             Binary; convert 1, ..., 4159 to base 2.
   Ḍ            Undecimal; convert each digit array from base 10 to integer.
                This generates the array A of all positive integers up to n²
                whose decimal representations consist entirely of 1's and 0's.
    9×þ         9 multiply table; for each x in A, yield [x, 2x, ..., 8x, 9x].
       F        Flatten; concatenate the resulting arrays, yielding the vector
                V. Note that V contains all numbers that match the regex ^d[0d]*$
                in base 10, in ascending order.
          ¹     Identity; yield n.
        %Þ      Sort the entries for V by their remainders modulo n. This places
                multiples of n at the beginning. The sorting algorithm in stable,
                so the first element of sorted V is the smallest multiple of n.
           Ḣ    Head; extract the first element.
            Q   Unique; deduplicate its digits in base 10. This yields [d, 0].
             S  Take the sum, yielding d.

5

JavaScript (ES6), 83 byte

n=>{for(p=1;;p=k)for(d=0;d++<9;)for(k=p;k<p+p;k++)if(k.toString(2)*d%n<1)return d;}

Ora ritorna 6per n=252! Ho provato un approccio ricorsivo ma è anche 83 byte e si blocca per me per i numeri più difficili:

f=(n,p=1,d=1,k=p)=>k<p+p?k.toString(2)*d%n<1?d:f(n,p,d,k+1):d>8?f(n,p+p):f(n,p,d+1)

4

Mathematica, 103 100 97 byte

#&@@IntegerDigits[Sort[Join@@Table[Cases[FromDigits/@{0,i}~Tuples~13/#,_Integer],{i,9}]][[10]]#]&


trova 317 in 0,39 sec

Provalo online copia / incolla il codice, aggiungi [317] alla fine e premi MAIUSC + INVIO per eseguire

-3 byte da @JungHwan Min
-3 byte da @Keyu Gan


Si può sbarazzarsi di *a *#, e Tuples[{0,i},13]è{0,i}~Tuples~13
JungHwan Min

sì, certo. Fatto!
J42161217

Oh, e ancora uno: [[1]]alla fine è lo stesso che mettere #&@@all'inizio
JungHwan Min

... e siamo arrivati ​​a 100! grazie per -3 byte
J42161217

Puoi usare Join@@invece diFlatten@
Keyu Gan il

2

Python 2/3, 129 128 127 byte

from itertools import*
lambda n:next(d for p in count()for d in range(1,10)for k in range(2**p,2*2**p)if d*int(bin(k)[2:])%n<1)

-1 byte: count(0)count()
-1 byte: ==0<1poiché non può essere negativo


2

Gelatina , 21 byte

9Rṭ€0ṗ€⁴ẎḌḍ@Ðf⁸ṢDFḟ0Ḣ

Un collegamento monadico che restituisce il numero O un programma completo che lo stampa.

Un bruto-forcer a raggio limitato che impiega meno di 20 secondi per ogni 1 ≤ n ≤ 500 (meno di 3 secondi per un costo di 1 byte in codice - sostituirlo con 13).

Provalo online!

Come?

9Rṭ€0ṗ€⁴ẎḌḍ@Ðf⁸ṢDFḟ0Ḣ - Link: number, n
9R                    - range of 9 = [1,2,3,4,5,6,7,8,9]
  ṭ€0                 - tack €ach to 0 -> [[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[0,7],[0,8],[0,9]]
       ⁴              - literal 16
     ṗ€               - Cartesian product for €ach
        Ẏ             - tighten (flatten by 1 level)
         Ḍ            - covert from decimal list to number (vectorises)
              ⁸       - chain's left argument (n)
            Ðf        - filter keep items for which this yields a truthy value:
          ḍ@          -   divisible? with swapped @rguments
               Ṣ      - sort
                D     - convert to decimal list (vectorises)
                 F    - flatten into a single list
                  ḟ0  - filter out zeros
                    Ḣ - head (get the first value)

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.