# n * k = dd0d00d where d = ...?

Given a positive integer n ≤ 500:

• Find the smallest positive integer k such that all digits in the decimal representation of n*k are either 0 or d, with 1 ≤ d ≤ 9.

• Print or return d in less than 30 seconds (read more about that in the Clarifications and rules section).

## Easy examples

Here are the first 30 values of 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 |
+----+-------+---------+---+    +----+-------+---------+---+


## Not-so-easy examples

One particularity of this challenge is that some values are much harder to find than others -- at least with a purely brute-force approach. Below are some examples of n that lead to a high value of 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 |
+-----+------------+---------------+---+    +-----+------------+---------------+---+


## Clarifications and rules

• n*k will always contain at least one digit d, but it may contain no zero at all.
• This is , so the shortest code in bytes wins. However, your program or function must be able to return the result for any 1 ≤ n ≤ 500 in less than 30 seconds on middle-range hardware.
• Keep in mind that some values are harder to find than others. A program that would try to brute-force the value of k is unlikely to comply with the time-limit constraint (a good test case is n = 317). There are significantly faster methods to find d.

## Reference table

All values of d for 1 ≤ n ≤ 500 are listed below.

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

• Loosely inspired by (but quite different from) this recent challenge. Jul 6, 2017 at 18:25
• n=6669666 -> d=9 Jul 6, 2017 at 22:07
• Interesting diagonals in that table. Jul 6, 2017 at 23:00
• @James Indeed. The patterns would appear a bit more clearly by formatting MOD 24. With MOD 25, we get some diagonals instead. :-) Jul 8, 2017 at 10:22

## JavaScript (ES6), 83 bytes

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


Now returns 6 for n=252! I tried a recursive approach but it's also 83 bytes and crashes out for me for the harder numbers:

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)


# Mathematica, 103100 97 bytes

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


finds 317 in 0.39 sec

Try it online copy/paste the code, add [317] at the end and press shift+enter to run

-3 bytes from @JungHwan Min
-3 bytes from @Keyu Gan

• You can get rid of * in *#, and Tuples[{0,i},13] is {0,i}~Tuples~13 Jul 6, 2017 at 23:14
• yes, of course.done! Jul 6, 2017 at 23:19
• Oh, and one more: [[1]] at the end is the same as putting #&@@ at the beginning Jul 7, 2017 at 0:43
• ...and we made it to 100! thanks for -3 bytes Jul 7, 2017 at 5:04
• You may use Join@@ instead of Flatten@ Jul 7, 2017 at 5:51

# Jelly, 1615 14 bytes

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


Quadratic runtime (under 25 seconds on TIO).

Try it online!

### Alternate version, 15 bytes

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


Constant runtime (approx. 1 second on TIO).

Try it online!

### How it works

²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.  # Python 2/3, 129128 127 bytes 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<1 since it can't be negative # Jelly, 21 bytes 9Rṭ€0ṗ€⁴ẎḌḍ@Ðf⁸ṢDFḟ0Ḣ  A monadic link returning the number OR a full program printing it. A limited-range brute-forcer which takes less than 20 seconds for any 1 ≤ n ≤ 500 (less than 3 seconds for a 1 byte code cost - replace ⁴ with 13). Try it online! ### How? 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)  # PHP, 87 bytes for(;++$i<5e3;)for($n=10;$d=--$n*decbin($i);)($y&&$d>$y)|$d%$argn?:$x=$n.!$y=$d;echo$x;


Try it online!

# PHP, 89 bytes

for(;++$i<5e3;)for($n=10;$d=--$n*decbin($i);)$d%$argn?:$r[$d]=$n;krsort($r);echo end($r);


Try it online!