Given an input list of 6 distinct digits, find 3 numbers a, b and c such that a × b = c, with a having 2 digits, b having 1 digit and c having 3 digits. In a more visual way, your program must arrange those 6 digits in the boxes of this image:

enter image description here

If more than one solution exists, you may output any of them.


6 distinct digits. You may take them in any reasonable way for your language.


The 3 numbers a, b and c. The output format is relatively free, as long as the 3 numbers are separated and are always printed in the same order (but not necessarily in the order a, b, c).

Test Cases

1, 2, 3, 4, 5, 6 -> 54,3,162  
2, 3, 4, 5, 6, 7 -> 57,6,342 or 52,7,364


The shortest code in bytes wins.

  • 1
    \$\begingroup\$ That's the first thing I thought off when I saw the challenge too @Dada... I suggest putting it in the sandbox to get some feedback before posting it :-) \$\endgroup\$ – Stewie Griffin Feb 16 '17 at 13:50
  • 1
    \$\begingroup\$ Is the input guaranteed to result in a solution? \$\endgroup\$ – AdmBorkBork Feb 16 '17 at 14:32
  • 1
    \$\begingroup\$ I have edited the wording of your challenge quite a bit to make it clear (in my opinion). Make sure I didn't change the goal of the challenge. \$\endgroup\$ – Fatalize Feb 16 '17 at 14:59
  • 1
    \$\begingroup\$ I also think the challenge needs a more explicit title, but I'm out of ideas right now. \$\endgroup\$ – Fatalize Feb 16 '17 at 15:01
  • 1
    \$\begingroup\$ Should an input of 0,1,2,3,4,5 result in 13,4,052; no solution; or is any behaviour OK? \$\endgroup\$ – Jonathan Allan Feb 17 '17 at 4:28

13 Answers 13


Brachylog (2), 10 bytes


Try it online!

Far too slow to run in a reasonable length of time (the Brachylog interpreter spends a long time doing multiplications on empty strings, 4-digit numbers, negative numbers etc. using a very slow constraint solver). The TIO link uses an input with only 3 digits (this program can handle input with any number of digits). This is a function whose input is a number containing all the digits required (e.g. 234567) – the lack of duplicates in the input means that you can always just put any 0 at the end to avoid a leading zero – and whose output is a list in the order [b, a, c] (e.g. [6, 57, 342]).


p           Permute the digits of the input
 ~c₃        Split them into three groups
    o       Sort the three groups
     .      to produce the output, which must have the following property:
      k     all but the last group
       ×    when multiplied together
        ~t  produces the last group

So where did the requirement on the groups to be 2, 1, and 3 digits go? Well, we know there are 6 digits in the input, and the groups are in sorted orders. The only possible sizes they can have, therefore, are [1, 1, 4], [1, 2, 3], or [2, 2, 2]. The first case is impossible (you can't multiply two 1-digit numbers to produce a 4-digit number, as 9×9 is only 81), as is the last case (you can't multiply two 2-digit numbers to produce a 2-digit number, as even 10×10 produces 100). Thus, the return values [b, a, c] must be 1, 2, and 3 digits long in that order, so a is 2 digits, b is 1 digit, and c is 3 digits, as requested.

  • 2
    \$\begingroup\$ Well...I surrender \$\endgroup\$ – Fatalize Feb 16 '17 at 17:32

JavaScript (ES6), 90 88 bytes

Takes input as an array of 6 digits. Returns a string describing a possible solution (such as '54*3==162') or exits with a 'too much recursion' error if (and only if) there's no solution.


How it works

This is a deterministic algorithm.

The primes P=2 and Q=3779 were chosen in such a way that the sort callback (k = k * P % Q) & 2 is guaranteed to generate all 720 possible permutations of the input array over time. More precisely, all permutations are covered after 2798 sorts -- which should be within the recursion limit of all browsers.

We inject each permutation in the expression 01*2==345 by mapping the digits to the corresponding entries in the array.

We evaluate this expression and do recursive calls until it's true.



console.log(f([1,2,3,4,5,6])); // 54*3==162
console.log(f([2,3,4,5,6,7])); // 52*7==364
console.log(f([1,2,3,4,6,7])); // 73*2==146

  • \$\begingroup\$ Assuming the output format is still valid, use - instead of == (and reverse the ?:) to save a byte. \$\endgroup\$ – Neil Feb 16 '17 at 20:07
  • 1
    \$\begingroup\$ @Neil Actually, I made the same suggestion to zeppelin. I probably should golf it some more as well, but I must admit that I like the current output format. \$\endgroup\$ – Arnauld Feb 16 '17 at 23:58
  • \$\begingroup\$ Did you bruteforce to find 3379, or did you use mathematical reasoning? If so, could provide your way of finding it? :) \$\endgroup\$ – Yytsi Feb 20 '17 at 13:36
  • \$\begingroup\$ @TuukkaX Nothing really fancy here. I just bruteforced it, my criteria being 1) as few digits as possible for P and Q and 2) as few sort iterations as possible. \$\endgroup\$ – Arnauld Feb 20 '17 at 14:13

Brachylog, 17 bytes


Try it online!


p                   Try a permutation of the Input
 ~c[Ċ,I,Ṫ]          Deconcatenate it; the result must be a list of the form [[_,_],_,[_,_,_]]
          cᵐ.       Output is the list of integers you get when mapping concatenate on the
                      previous list
             k×~t   The first two ints of the Output, when multiplied, result in the third
                      int of the Output

05AB1E, 15 13 bytes

Saved two bytes thanks to Emigna!


Uses the CP-1252 encoding. Try it online!


œ                 # Get all permutations of the input
 J                # Join them to get the numbers
  vy              # For each element in the list..
    3L            #   Push the list [1, 2, 3]
      £           #   Pops a and pushes [a[0:1], a[1:3], a[3:6]]
       Â`         #   Bifurcate and flatten
         *        #   Multiply the top two elements in the stack
          Qi      #   If equal to the third element..
            ,     #     Print the array
  • \$\begingroup\$ You can replace 213S with 3L as the order didn't have to be 2,1,3 according to the specs. \$\endgroup\$ – Emigna Feb 16 '17 at 15:17
  • \$\begingroup\$ Good to know that £ cumulatively vectorizes... If that's the correct way to say that. \$\endgroup\$ – Magic Octopus Urn Feb 16 '17 at 20:32

Bash + coreutils, 70

a=`shuf -ze $@`
echo $b

No particularly easy way to generate all the permutations. Instead randomly generate permutations and calculate until we find a good one.

Output is in the form A*B-C - i.e. the expression that will evaluate to zero when we have the correct permutation.

Try it online.


CJam, 23 bytes


Try it online!


Python 2, 105 bytes

lambda s:[(x[0],x[1:3],x[3:])for x in permutations(s)if eval('%s*%s%s==%s%s%s'%x)]
from itertools import*

Try it online!

88 bytes solution with a more flexible output

lambda s:[x for x in permutations(s)if eval('%s*%s%s==%s%s%s'%x)]
from itertools import*

Try it online!
where the output would be ['6', '5', '7', '3', '4', '2'] instead '6', '57', '342'

  • 2
    \$\begingroup\$ You didn't put your import at the top... shakes head \$\endgroup\$ – mbomb007 Feb 16 '17 at 19:28
  • \$\begingroup\$ @mbomb007 gotta work on TIO ¯\_(ツ)_/¯ \$\endgroup\$ – Rod Feb 17 '17 at 2:32
  • \$\begingroup\$ You're the first person I've seen who actually put the f= in the header. It's not a big deal. \$\endgroup\$ – mbomb007 Feb 17 '17 at 14:27

PHP, 110 bytes

It'll get there... eventually...

<?$v=$argv;unset($v[0]);do shuffle($v)&[$a,$b,$c,$d,$e,$f]=$v;while("$a$b"*$c!="$d$e$f");echo"$a$b $c $d$e$f";


echo"$a$b $c $d$e$f";

PHP, 77 bytes


Takes input as a string.


Pip, 18 bytes

17 bytes of code, +1 for -S flag.


Takes input as a string of digits via command-line argument. Output is in the order c, b, a. Try it online!

This code outputs all solutions if multiple exist. If it is required to output only one solution, then add three bytes and wrap the program in (...0).


                   a is 1st cmdline arg (implicit)
              PMa  Compute all permutations of a
             M     To each, map this function:
          3,5       Range(3,5)--contains values 3 and 4
       _^@          Split the function argument at those indices
                    This transforms a string like 342657 into a list [342; 6; 57]
     FI            Now filter the list of split permutations on this function:
$/_                 Fold on division: takes 1st element and divides it by the rest
   =1               Compare the quotient with 1
                    This keeps only the permutations where the first number is the product
                    of the other two
                   Autoprint the list (implicit), with each sublist on a separate line
                   and space-separated (-S flag)

ES6 (Javascript), 85, 82, 79 bytes

Accepts an array of digits (strings), returns a 3-element array [A,B,C] => C=A*B




  • Saved 3 more bytes by reusing d and a, and getting rid of == (Thanks @Arnauld !)
  • Saved 3 bytes using destructuring assignment

Try It !


function generate(A) {
<input type="text" id="A" value="123456"/><button onclick="generate(A.value)">GENERATE</button>

  • \$\begingroup\$ Can you guarantee that your random sort will actually cover all permutations? \$\endgroup\$ – Neil Feb 16 '17 at 20:09
  • \$\begingroup\$ @Neil, if you are looking for strict formal proof, I don't think I can provide you with one, but empirically it does result in a pretty much uniform distribution of permutations. \$\endgroup\$ – zeppelin Feb 16 '17 at 21:08

Ruby, 60 bytes

->x{x.permutation{|a|(eval a="%d%d*%d==%d%d%d"%a)&&puts(a)}}

Prints all solutions as "a*b==c"


->x{x.permutation{|a|(eval a="%d%d*%d==%d%d%d"%a)&&puts(a)}}[[1,2,3,4,5,6]]

->x{x.permutation{|a|(eval a="%d%d*%d==%d%d%d"%a)&&puts(a)}}[[2,3,4,5,6,7]]

Batch, 305 bytes

@echo off
for /l %%i in (0,1,719)do set n=%%i&call:c
set t=%d%
set s=
for /l %%j in (6,-1,1)do set/ap=n%%%%j,n/=%%j&call:l
set s=%s:~0,2%*%s:~2,1%-%s:~3%
if %n%==0 echo %s%
call set u=%%t:~%p%%%
call set s=%%s%%%%u:~,1%%
call set t=%%t:~,%p%%%%%u:~1%%

Takes input on STDIN as a string [1-9]{6} and outputs all solutions in dd*d-ddd format. Batch isn't very good at string manipulation so generating the 720 permutations is a little awkward.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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