Given a positive number \$n\$ we call another (not same as n) positive number \$m\$ good if we insert same digits in both n and m and the resulting fractional value is same.

$$m/n = m_{\text{transformed}}/n_{\text{transformed}}$$ $$or$$ $$m*n_{\text{transformed}} = m_{\text{transformed}}*n$$


  • No leading zeros or trailing zero insertion is allowed (you can insert 0 at beginning or end, but this must not be the last digit added in that end),other than this you can insert any digit(same digit and same number of time in both) at any place,i.e you need not insert at same place in n and m.

  • At least one insertion is required.

Examples :

Example 1: $$n= 12$$

here m=66 is a good number ,we can obtain it by inserting 1 in m and n such that they become 616 and 112.

Example 2:


here m=60 is a good number, we can get 300 and 600 by inserting 0 in middle, as mentioned we cannot insert 0 at end or beginning, but here we inserted it in middle.

Example 3:

$$n=4$$ here 7 is a good number, as we can insert 2 and 1 , making them 124 and 217 respectively, now the fractional value is same.

Example 4:

$$n=11$$ here 2 is a good number, as we can insert 2 and 2 , making them 1221 and 222 respectively, now the fractional value is same.

Example 5:

$$n=4269$$ here 1423 is a good number, as we can insert 0, making them 42069 and 14023 respectively, now the fractional value is same.

Example 6:

$$n=1331$$ here 242 is a good number, as we can insert 2 and 2, making them 123321 and 22422 respectively, now the fractional value is same.


You have to find out smallest good number for a given n (there always exists a good number as pointed out by a user in comments).

This is , so the shortest answer in bytes per language wins.

  • 9
    \$\begingroup\$ A solution must always exist. Given \$n\$ choose \$m=n\times 10\$, now insert a \$1\$ at the beginning of both numbers (or really do any insertion at the same place relative to the beginning of the number). \$\endgroup\$
    – Wheat Wizard
    Jun 7, 2021 at 11:30
  • 1
    \$\begingroup\$ Also, 3 is a valid solution for n=12: (12, 3) -> (132, 33), just helping you there \$\endgroup\$
    – user100690
    Jun 7, 2021 at 12:03
  • 5
    \$\begingroup\$ I'd suggest that you update your testcases such that they show the smallest possible number rather than the current ones which are not necessarily the shortest. \$\endgroup\$
    – user100690
    Jun 7, 2021 at 15:12
  • 1
    \$\begingroup\$ Also, when we insert 0s, sometimes the result is equivalent to adding 0s at the end, should this one not be allowed too ? For example, if we add two 0s in the middle of 10, we get 1000 which is equivalent to adding the 0s to the end \$\endgroup\$ Jun 7, 2021 at 20:59
  • 2
    \$\begingroup\$ @OldMan Maybe make it such that only 1 digit insertion is allowed? \$\endgroup\$
    – user100690
    Jun 13, 2021 at 8:53

2 Answers 2


Haskell, 154 bytes

g n=head[m|m<-[1..n*10],m/=n,w<-i m,v<-i n,w*n==v*m]
i m=[read$take n s++c:drop n s|let s=show m,let l=length s,n<-[0..l],c<-"123456789"++['0'|n/=0,n/=l]]

Try it online!


JavaScript, 8 bytes


Try it online!

Takes \$n\$ as a BigInt. Returns 1 for \$n \ne 1\$, 2 for \$n=1\$: 1 is good with every other number.

For example, 1 is good with 2 as follows:

1052631578947368421 * 2 = 

This can be constructed by starting with the digit 1 at the top right, and doing the multiplication by 2 while copying each digit of the product up and to the left, until it finishes by arriving at a 2 with no carry.

This always works. Let \$x\$ be the number formed by the inserted digits, and \$l\$ be the number of inserted digits. Then we need \$(10x + 1)*n = n*10^l + x\$, which rearranges to \$(10n - 1)*x = n*10^l - 1\$. This always has suitable integer solutions: by Euler's theorem, \$l = \varphi(10n - 1) + 1\$ works.

However, this inserts an ending zero (which is not allowed) when \$n\$ has an ending zero. A solution is to ignore all ending zeroes of \$n\$, then put them back at the end; for example, for \$n=200\$:

  1052631578947368421 * 200 = 

Here's a function that does the full calculation, returning \$[m, n_\mathrm{transformed}, m_\mathrm{transformed}]\$:

JavaScript, 86 bytes


Try it online!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.