22
\$\begingroup\$

Objective

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.

Input

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

Output

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

Scoring

The shortest code in bytes wins.

\$\endgroup\$
  • 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

8
\$\begingroup\$

Brachylog (2), 10 bytes

p~c₃o.k×~t

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]).

Explanation

p~c₃o.k×~t
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.

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

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.

f=(a,k=1)=>eval(s='01*2==345'.replace(/\d/g,n=>a[n],a.sort(_=>(k=k*2%3779)&2)))?s:f(a,k)

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.

Test

f=(a,k=1)=>eval(s='01*2==345'.replace(/\d/g,n=>a[n],a.sort(_=>(k=k*2%3779)&2)))?s:f(a,k)

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

\$\endgroup\$
  • \$\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
6
\$\begingroup\$

Brachylog, 17 bytes

p~c[Ċ,I,Ṫ]cᵐ.k×~t

Try it online!

Explanation

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
\$\endgroup\$
3
\$\begingroup\$

05AB1E, 15 13 bytes

Saved two bytes thanks to Emigna!

œJvy3L£Â`*Qi,

Uses the CP-1252 encoding. Try it online!

Explanation:

œ                 # 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
\$\endgroup\$
  • \$\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
3
\$\begingroup\$

Bash + coreutils, 70

for((b=1;b;));{
a=`shuf -ze $@`
b=${a:0:2}*${a:2:1}-${a:3:3}
}
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.

\$\endgroup\$
2
\$\begingroup\$

CJam, 23 bytes

qm!{3/)2/+:i}%{(\:*=}=p

Try it online!

\$\endgroup\$
2
\$\begingroup\$

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'

\$\endgroup\$
  • 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
2
\$\begingroup\$

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

Ungolfed:

<?
$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";
\$\endgroup\$
2
\$\begingroup\$

PHP, 77 bytes

for(;;)eval(strtr('0.*1-"428"||die("0.,1,428");',1/7,str_shuffle($argv[1])));

Takes input as a string.

\$\endgroup\$
1
\$\begingroup\$

ES6 (Javascript), 85, 82, 79 bytes

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

Golfed

R=(w,[a,b,c,d,e,f]=w)=>f*(d+=e)^(a+=b+c)?R(w.sort(_=>Math.random()-.5)):[a,d,f]

EDITS:

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

Try It !

R=(w,[a,b,c,d,e,f]=w)=>f*(d+=e)^(a+=b+c)?R(w.sort(_=>Math.random()-.5)):[a,d,f];

function generate(A) {
   console.log(R([...A]));
}
<input type="text" id="A" value="123456"/><button onclick="generate(A.value)">GENERATE</button>

\$\endgroup\$
  • \$\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
1
\$\begingroup\$

Pip, 18 bytes

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

$/_=1FI_^@3,5MPMa

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).

Explanation

                   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)
\$\endgroup\$
1
\$\begingroup\$

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"

Example:

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

->x{x.permutation{|a|(eval a="%d%d*%d==%d%d%d"%a)&&puts(a)}}[[2,3,4,5,6,7]]
52*7==364
57*6==342
\$\endgroup\$
1
\$\begingroup\$

Batch, 305 bytes

@echo off
set/pd=
for /l %%i in (0,1,719)do set n=%%i&call:c
exit/b
: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%
set/an=%s%
if %n%==0 echo %s%
exit/b
:l
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.

\$\endgroup\$

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.