Re: Make 100 by using + - x / and 1~9
- From: Alfred Heiligenbrunner <xyz@xxxxxx>
- Date: Fri, 26 Oct 2007 10:41:55 +0200
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?
1*2*3*4+5+6+7*8+9=100
1-2+3*4*5+6*7+8-9=100
1-2+3*4*5-6+7*8-9=100
1+2+3+4+5+6+7+8*9=100
1*2*3+4+5+6+7+8*9=100
1-2*3+4*5+6+7+8*9=100
1+2*3+4*5-6+7+8*9=100
1-2*3-4+5*6+7+8*9=100
1+2-3*4+5*6+7+8*9=100
1+2*3*4*5/6+7+8*9=100
1*2*3*4+5+6-7+8*9=100
1-2*3-4-5+6*7+8*9=100
1+2-3*4-5+6*7+8*9=100
1+2+3-4*5+6*7+8*9=100
1*2*3-4*5+6*7+8*9=100
Source code: C++
#include <iostream>
int main()
{
char ops[] = "+-*/";
for(long i = 0; i<= 4*4*4*4*4*4*4*4-1; i++)
{
long j = i;
std::cout << "1";
for(int k = 2; k <= 9; k++)
{
std::cout << ops[j & 3] << k;
j>>=2;
}
std::cout << endl;
}
return 0;
}
Source code: Perl
my $a;
while(<>)
{
chomp;
eval('$a='.$_);
print "$_=$a\n" if $a==100;
}
Congrats, Kira, nice, short and quick code.
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.
Regards,
Alfred
.
- Follow-Ups:
- Re: Make 100 by using + - x / and 1~9
- From: Bill Dubuque
- Re: Make 100 by using + - x / and 1~9
- From: Bill Dubuque
- Re: Make 100 by using + - x / and 1~9
- From: Alfred Heiligenbrunner
- Re: Make 100 by using + - x / and 1~9
- From: Kira Yamato
- Re: Make 100 by using + - x / and 1~9
- References:
- Re: Make 100 by using + - x / and 1~9
- From: Kira Yamato
- Re: Make 100 by using + - x / and 1~9
- From: Kira Yamato
- Re: Make 100 by using + - x / and 1~9
- Prev by Date: A simple question about a Riemann surface, tommy 1729 did not answer to [ was: Multivalued Logarithm ]
- Next by Date: Re: Minimization point
- Previous by thread: Re: Make 100 by using + - x / and 1~9
- Next by thread: Re: Make 100 by using + - x / and 1~9
- Index(es):
Relevant Pages
|