One of the games of the French TV show Des chiffres et des lettres, called Le compte est bon ('The total is right'), consists of the following :

You get a number A between 101 and 999 as well as six numbers E1,..,E6 chosen randomly, with possible repetitions, from {1,2,3,4,5,6,7,8,9,10,25,50,75,100}

The goal is to get as close to A as possible by using the operations +,-,*,/ on E1,...,E6. Each number may only be used once, but you do not have to use them all. The result of each operation must be a positive integer

Example : A=859, E1,..,E6= 6, 75, 3, 6, 8, 9 -> Best result : 858=(((75+6)-(9-6))*(8+3))

Following that concept, here are the rules :

  • Write a function that takes as argument a positive integer A and a list of positive integers L. The function returns a string showing the closest one can get to A, which we'll call B, using the numbers in L, in the format B = operations (ex for A=859 : "858=(((75+6)-(9-6))*(8+3)))", you may reduce the number of parenthesis as long as it remains clear).

  • The only allowed operations are +,-,* and /. The result of each of the operations must be positive integers.

  • Each number of L may only be used once, but you don't have to use all the numbers.

This is code golf, the shortest code in bytes wins !

  • \$\begingroup\$ Out of curiosity, what are the total number of possible answers given 6 numbers and 4 operators? \$\endgroup\$
    – Compass
    Commented Nov 25, 2014 at 16:00
  • \$\begingroup\$ @Compass I'm not sure, but the program I made in Python runs about 1500000 iterations (It's a pretty basic one) \$\endgroup\$ Commented Nov 25, 2014 at 16:04
  • 1
    \$\begingroup\$ It's not actually a duplicate, as this one is more constrained. A winning answer here will probably not be general enough to qualify there, and a qualifying answer there will probably not win here. Related, but not the same. \$\endgroup\$ Commented Jun 11, 2015 at 17:29


Browse other questions tagged or ask your own question.