Re: Make 100 by using + - x / and 1~9



Alfred Heiligenbrunner schrieb am 26.10.2007 10:41:
Kira Yamato schrieb am 26.10.2007 00:41:
On 2007-10-25 18:35:42 -0400, Kira Yamato <kirakun@xxxxxxxxxxxxx> said:

On 2007-10-25 17:22:50 -0400, 131208@xxxxxxxxx said:

1 ( ) 2 ( ) 3 ( ) 4 ( ) 5 ( ) 6 ( ) 7 ( ) 8 ( ) 9 = 100


( ) only can be among +, - , x, /

The empty space is not allowed.

How many solutions are there?



Here a solution in Mathematica:

In[1]:=
Select[Flatten[
Table[
Inner[StringJoin, {"1", "2", "3", "4", "5", "6", "7", "8"},
{"+", "-", "*", "/"}[[{i1, i2, i3, i4, i5, i6, i7, i8}]],
StringJoin]
<> "9",
{i1, 4}, {i2, 4}, {i3, 4}, {i4, 4}, {i5, 4}, {i6, 4}, {i7, 4}, {i8, 4}]],
(ToExpression[#] == 100) &]

Out[1]=
{"1+2+3+4+5+6+7+8*9", "1+2+3-4*5+6*7+8*9", "1+2-3*4+5*6+7+8*9",
"1+2-3*4-5+6*7+8*9", "1+2*3+4*5-6+7+8*9", "1+2*3*4*5/6+7+8*9",
"1-2+3*4*5+6*7+8-9", "1-2+3*4*5-6+7*8-9", "1-2*3+4*5+6+7+8*9",
"1-2*3-4+5*6+7+8*9", "1-2*3-4-5+6*7+8*9", "1*2*3+4+5+6+7+8*9",
"1*2*3-4*5+6*7+8*9", "1*2*3*4+5+6+7*8+9", "1*2*3*4+5+6-7+8*9"}

Although evaluation time is rather short (6 seconds on a Pentium 4, 3 GHz, 1.5 GB RAM) I wonder, if there is not a more efficient or more elegant code.

At least _shorter_ is the following Mathematica code:

Select[
Map[StringJoin["1", #] &,
Inner[StringJoin,
Map[{"+", "-", "*", "/"}[[#]] &,
Flatten[Array[List, Table[4, {8}]], 7]],
Table[ToString[i], {i, 2, 9}], StringJoin, 2]],
(ToExpression[#] == 100) &]

Here "Array[...]" creates all the 65536 possible 8-tuples {1,1,...,1} to {4,4,...,4}, to be used as indeces to {"+", "-", "*", "/"}.
.


Loading