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



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
.



Relevant Pages

  • Re: ping source code
    ... >I was reading through original source code of ping for some insight ... and types (defaulting to int if not specified) given between the ... the older type of declaration; I'm not sure about C99 - implicit int is ...
    (comp.lang.c)
  • Re: C objects
    ... >void foo{ ... >int i; ... An identifier is a lexical concept and is ... >talking about source code (the only part we have any real control ...
    (comp.lang.c)
  • Re: dh, the daemon helper
    ... You can download the C source code, ... line 52 I think most C programmers prefer const char over char ... open_max a long instead of an int though. ...
    (comp.unix.programmer)
  • Re: Simple Welding of two small C/C++ Programs, But How??
    ... >> it but I get compiler errors. ... >> statement to an existing single program. ... > to help you create or assemble source code together to meet requirements. ... > int main ...
    (alt.comp.lang.learn.c-cpp)
  • Re: US Military Dead during Iraq War
    ... >>> Please post your source code. ... >programs are much easier to read than assembler programs. ... void print_xlat1(int* xl, int *h) ... frequency for know the first file *frewuency order* ...
    (alt.lang.asm)