This article is from the Puzzles FAQ, by Chris Cole chris@questrel.questrel.com and Matthew Daly mwdaly@pobox.com with numerous contributions by others.
In how many ways can "." be replaced with "+", "-", or "" (concatenate) in
.1.2.3.4.5.6.7.8.9=1 to form a correct equation?
arithmetic/digits/equations/123456789.s
1-2 3+4 5+6 7-8 9 = 1 +1-2 3+4 5+6 7-8 9 = 1 1+2 3+4-5+6 7-8 9 = 1 +1+2 3+4-5+6 7-8 9 = 1 -1+2 3-4+5+6 7-8 9 = 1 1+2 3-4 5-6 7+8 9 = 1 +1+2 3-4 5-6 7+8 9 = 1 1-2 3-4+5-6 7+8 9 = 1 +1-2 3-4+5-6 7+8 9 = 1 1-2-3-4 5+6 7-8-9 = 1 +1-2-3-4 5+6 7-8-9 = 1 1+2-3 4+5 6-7-8-9 = 1 +1+2-3 4+5 6-7-8-9 = 1 -1+2 3+4+5-6-7-8-9 = 1 -1 2+3 4-5-6+7-8-9 = 1 1+2+3+4-5+6+7-8-9 = 1 +1+2+3+4-5+6+7-8-9 = 1 -1+2+3-4+5+6+7-8-9 = 1 1-2-3+4+5+6+7-8-9 = 1 +1-2-3+4+5+6+7-8-9 = 1 1+2 3+4 5-6 7+8-9 = 1 +1+2 3+4 5-6 7+8-9 = 1 1+2 3-4-5-6-7+8-9 = 1 +1+2 3-4-5-6-7+8-9 = 1 1+2+3+4+5-6-7+8-9 = 1 +1+2+3+4+5-6-7+8-9 = 1 -1+2+3+4-5+6-7+8-9 = 1 1-2+3-4+5+6-7+8-9 = 1 +1-2+3-4+5+6-7+8-9 = 1 -1-2-3+4+5+6-7+8-9 = 1 1-2+3+4-5-6+7+8-9 = 1 +1-2+3+4-5-6+7+8-9 = 1 1+2-3-4+5-6+7+8-9 = 1 +1+2-3-4+5-6+7+8-9 = 1 -1-2+3-4+5-6+7+8-9 = 1 -1+2-3-4-5+6+7+8-9 = 1 -1+2 3+4 5-6 7-8+9 = 1 1-2 3-4 5+6 7-8+9 = 1 +1-2 3-4 5+6 7-8+9 = 1 -1+2 3-4-5-6-7-8+9 = 1 -1+2+3+4+5-6-7-8+9 = 1 1-2+3+4-5+6-7-8+9 = 1 +1-2+3+4-5+6-7-8+9 = 1 1+2-3-4+5+6-7-8+9 = 1 +1+2-3-4+5+6-7-8+9 = 1 -1-2+3-4+5+6-7-8+9 = 1 1+2-3+4-5-6+7-8+9 = 1 +1+2-3+4-5-6+7-8+9 = 1 -1-2+3+4-5-6+7-8+9 = 1 -1+2-3-4+5-6+7-8+9 = 1 1-2-3-4-5+6+7-8+9 = 1 +1-2-3-4-5+6+7-8+9 = 1 1-2 3+4+5+6+7-8+9 = 1 +1-2 3+4+5+6+7-8+9 = 1 1+2+3+4 5-6 7+8+9 = 1 +1+2+3+4 5-6 7+8+9 = 1 1 2+3 4+5-6 7+8+9 = 1 +1 2+3 4+5-6 7+8+9 = 1 1+2+3-4-5-6-7+8+9 = 1 +1+2+3-4-5-6-7+8+9 = 1 -1+2-3+4-5-6-7+8+9 = 1 1-2-3-4+5-6-7+8+9 = 1 +1-2-3-4+5-6-7+8+9 = 1 -1-2-3-4-5+6-7+8+9 = 1 -1-2 3+4+5+6-7+8+9 = 1 1-2+3 4-5 6+7+8+9 = 1 +1-2+3 4-5 6+7+8+9 = 1 1 2-3 4+5-6+7+8+9 = 1 +1 2-3 4+5-6+7+8+9 = 1Total solutions = 69 (26 of which have a leading "+", which is redundant) 69/19683 = 0.35 % for those who care (it's not very elegant but it did the trick):#include <stdio.h> #include <math.h> main (argc,argv) int argc; char *argv[]; { int sresult, result, operator[10],thisop; char buf[256],ops[3]; int i,j,tot=0,temp; ops[0] = ' '; ops[1] = '-'; ops[2] = '+'; for (i=1; i<10; i++) operator[i] = 0; for (j=0; j < 19683; j++) { result = 0; sresult = 0; thisop = 1; for (i=1; i<10; i++) { switch (operator[i]) { case 0: sresult = sresult * 10 + i; break; case 1: result = result + sresult * thisop; sresult = i; thisop = -1; break; case 2: result = result + sresult * thisop; sresult = i; thisop = 1; break; } } result = result + sresult * thisop; if (result == 1) { tot++; for (i=1;i<10;i++) printf("%c%d",ops[operator[i]],i); printf(" = %d\n",result); } temp = 0; operator[1] += 1; for (i=1;i<10;i++) { operator[i] += temp; if (operator[i] > 2) { operator[i] = 0; temp = 1;} else temp = 0; } } printf("Total solutions = %d\n" , tot); }
cwren@media.mit.edu (Christopher Wren) 
Continue to: