Connect Digits with Operators to Find Numbers

There are many ways to connect the digits 1 - 9 - 9 - 5. For example 1 + 9 + (9 - 5) is 14.

This program connects four digits in various combinations until it has found all the numbers from 1 to 100.

59 = ((1 + sqrt9) ^ sqrt9) - 5
60 = (sqrt(1 * 9) + 9) * 5
61 = 1 + ((9 + sqrt9) * 5)
62 = (19 * sqrt9) + 5
63 = (-19 * sqrt9) + 5!
64 = 19 + (9 * 5)

1995.c - source file

   1: #include <stdio.h>
   2: #include <stdlib.h>
   3: #include <errno.h>
   4: #include <math.h>
   5: #include <time.h>
   6: #include <string.h>
   7: 
   8: static int powers2[10] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512};
   9: static int Factorial[7] = {1, 1, 2, 6, 24, 120, 720};
  10: 
  11: #define NOPS2 5
  12: static char name2[NOPS2] = {'+', '-', '*', '/', '^'};
  13: 
  14: char *name1a[6];
  15: char *name1b[6];
  16: int moncode[6];
  17: int nops1;
  18: enum {NONE, NEG, SQRT, FACT, NEGSQRT, NEGFACT, FACTSQRT};
  19: 
  20: char *answer[100];
  21: #define putit(i,lin) answer[(i)-1] = dupstr (line)
  22: 
  23: static char *dupstr (char *str)
  24: {
  25:     char	*new;
  26:     
  27:     new = (char *) malloc (strlen (str) + 1);
  28:     if (!new) return NULL;
  29:     strcpy (new, str);
  30:     return new;
  31: } /* End of dupstr */
  32: 
  33: 
  34: static int f (double a, int op, double *x)
  35: {
  36:     double	b;
  37:     
  38:     switch (moncode[op])
  39:     {
  40:     case NONE :
  41: 	*x = a;
  42: 	return 1;
  43: 	
  44:     case NEG :
  45: 	*x = -a;
  46: 	return 1;
  47: 	
  48:     case SQRT :
  49: 	if ((int)a != a) return 0;	/* Don't bother with fractions */
  50: 	if (a <= 1.0) return 0;		/* Don't bother with sqrt(1) */
  51: 	*x = sqrt (a);
  52: 	if ((int)(*x) != *x) return 0;
  53: 	return 1;
  54: 	
  55:     case FACT :
  56: 	if (a < 1.0 || a > 6.0) return 0;
  57: 	if ((int)a != a) return 0;	/* Don't bother with fractions */
  58: 	*x = Factorial[(int)(a+0.5)];	/* Round off */
  59: 	return 1;
  60: 	
  61:     case NEGSQRT :
  62: 	if ((int)a != a) return 0;	/* Don't bother with fractions */
  63: 	if (a <= 1.0) return 0;		/* Don't bother with sqrt(1) */
  64: 	*x = -sqrt (a);
  65: 	if ((int)(*x) != *x) return 0;
  66: 	return 1;
  67: 	
  68:     case NEGFACT :
  69: 	if (a < 1.0 || a > 6.0) return 0;
  70: 	if ((int)a != a) return 0;	/* Don't bother with fractions */
  71: 	*x = -Factorial[(int)(a+0.5)];	/* Round off */
  72: 	return 1;
  73: 	
  74:     case FACTSQRT :
  75: 	if ((int)a != a) return 0;	/* Don't bother with fractions */
  76: 	if (a <= 1.0) return 0;		/* Don't bother with sqrt(1) */
  77: 	b = sqrt (a);
  78: 	if ((int)(b) != b) return 0;
  79: 	if (b < 1.0 || b > 6.0) return 0;
  80: 	*x = Factorial[(int)(b+0.5)];	/* Round off */
  81: 	return 1;
  82:     }
  83:     
  84:     return 0;
  85: } /* End of f */
  86: 
  87: 
  88: static int fn (double a, int op, double b, double *x)
  89: {
  90:     double	y;
  91:     
  92:     switch (op)
  93:     {
  94:     case 0 :
  95: 	*x = a + b;
  96: 	return 1;
  97: 	
  98:     case 1 :
  99: 	*x = a - b;
 100: 	return 1;
 101: 	
 102:     case 2 :
 103: 	*x = a * b;
 104: 	return 1;
 105: 	
 106:     case 3 :
 107: 	if (b == 0.0) return 0;
 108: 	*x = a / b;
 109: 	return 1;
 110: 	
 111:     case 4 :
 112: 	errno = 0;
 113: 	*x = pow (a, b);
 114: 	if (errno != 0) return 0;
 115: 	y = fabs (*x);
 116: 	if (y > 0.0 && y <1e-6) return 0;
 117: 	if (y > 1e6) return 0; 
 118: 	return 1;
 119:     }
 120:     return 0;
 121: } /* End of fn */
 122: 
 123: 
 124: static int tryit (double x)
 125: {
 126:     int		i;
 127:     
 128:     if (x >= 1.0 && x <= 100.0)
 129:     {
 130: 	i = (int) x;
 131: 	if (fabs (x-i) < 1e-10)
 132: 	{
 133: 	    if (answer[i-1] == NULL)
 134: 	    {
 135: 		/* answer[i-1] = dupstr (line); */
 136: 		return i;
 137: 	    }
 138: 	}
 139:     }
 140:     
 141:     return 0;
 142: } /* End of tryit */
 143: 
 144: 
 145: static void do1 (int a)
 146: {
 147:     double	a1;
 148:     int		index;
 149:     char	line[100];
 150:     int		ma;
 151:     
 152:     for (ma=0; ma<nops1; ma++)
 153:     {
 154: 	if (!f (a, ma, &a1)) continue;
 155: 	if ((index = tryit (a1)) != 0)
 156: 	{
 157: 	    sprintf (line, "%s%d%s", name1a[ma], a, name1b[ma]);
 158: 	    putit (index, line);
 159: 	}
 160:     }
 161: } /* End of do1 */
 162: 
 163: 
 164: static void do2 (int a, int b)
 165: {
 166:     double	a1, b1;
 167:     int		index;
 168:     char	line[100];
 169:     int		ma, mb, mz;
 170:     int		n;
 171:     double	z, z1;
 172:     
 173:     for (mz=0; mz<nops1; mz++)
 174:     {
 175: 	for (n=0; n<NOPS2; n++)
 176: 	{
 177: 	    for (ma=0; ma<nops1; ma++)
 178: 	    {
 179: 		if (!f (a, ma, &a1)) continue;
 180: 		for (mb=0; mb<nops1; mb++)
 181: 		{
 182: 		    if (!f (b, mb, &b1)) continue;
 183: 		    
 184: 		    if (!fn (a1, n, b1, &z)) continue;
 185: 		    if (!f (z, mz, &z1)) continue;
 186: 		    if ((index = tryit (z1)) != 0)
 187: 		    {
 188: 			sprintf (line, "%s%s%s%d%s %c %s%d%s%s%s",
 189: 			    name1a[mz], (mz == 0 ? "" : "["),
 190: 			    name1a[ma], a, name1b[ma],
 191: 			    name2[n],
 192: 			    name1a[mb], b, name1b[mb],
 193: 			    (mz == 0 ? "" : "]"), name1b[mz]);
 194: 			putit (index, line);
 195: 		    }
 196: 		}
 197: 	    }
 198: 	}
 199:     }
 200: } /* End of do2 */
 201: 
 202: 
 203: static void do3 (int a, int b, int c, int code)
 204: {
 205:     double	a1, b1, c1;
 206:     int		index;
 207:     char	line[100];
 208:     int		ma, mb, mc, mx, mz;
 209:     int		n1, n2;
 210:     double	x, x1, z, z1;
 211:     
 212:     for (mx=0; mx<nops1; mx++)
 213:     {
 214: 	for (mz=0; mz<nops1; mz++)
 215: 	{
 216: 	    for (n1=0; n1<NOPS2; n1++)
 217: 	    {
 218: 		for (n2=0; n2<NOPS2; n2++)
 219: 		{
 220: 		    for (ma=0; ma<nops1; ma++)
 221: 		    {
 222: 			if (!f (a, ma, &a1)) continue;
 223: 			for (mb=0; mb<nops1; mb++)
 224: 			{
 225: 			    if (!f (b, mb, &b1)) continue;
 226: 			    for (mc=0; mc<nops1; mc++)
 227: 			    {
 228: 				if (!f (c, mc, &c1)) continue;
 229: 				
 230: 				if (code == 1)
 231: 				{
 232: 				    if (!fn (a1, n1, b1, &x)) continue;
 233: 				    if (!f (x, mx, &x1)) continue;
 234: 				    if (!fn (x1, n2, c1, &z)) continue;
 235: 				    if (!f (z, mz, &z1)) continue;
 236: 				    if ((index = tryit (z1)) != 0)
 237: 				    {
 238: 					sprintf (line, "%s%s%s(%s%d%s %c %s%d%s)%s "
 239: 					    "%c %s%d%s%s",
 240: 					    name1a[mz], (mz == 0 ? "" : "["),
 241: 					    name1a[mx],
 242: 					    name1a[ma], a, name1b[ma],
 243: 					    name2[n1],
 244: 					    name1a[mb], b, name1b[mb],
 245: 					    name1b[mx],
 246: 					    name2[n2],
 247: 					    name1a[mc], c, name1b[mc],
 248: 					    (mz == 0 ? "" : "]"), name1b[mz]);
 249: 					putit (index, line);
 250: 				    }
 251: 				}
 252: 				else	/* code == 2 */
 253: 				{
 254: 				    if (!fn (b1, n2, c1, &x)) continue;
 255: 				    if (!f (x, mx, &x1)) continue;
 256: 				    if (!fn (a1, n1, x1, &z)) continue;
 257: 				    if (!f (z, mz, &z1)) continue;
 258: 				    if ((index = tryit (z1)) != 0)
 259: 				    {
 260: 					sprintf (line, "%s%s%s%d%s %c %s(%s%d%s %c "
 261: 					    "%s%d%s)%s%s%s",
 262: 					    name1a[mz], (mz == 0 ? "" : "["),
 263: 					    name1a[ma], a, name1b[ma],
 264: 					    name2[n1],
 265: 					    name1a[mx],
 266: 					    name1a[mb], b, name1b[mb],
 267: 					    name2[n2],
 268: 					    name1a[mc], c, name1b[mc],
 269: 					    name1b[mx],
 270: 					    (mz == 0 ? "" : "]"), name1b[mz]);
 271: 					putit (index, line);
 272: 				    }
 273: 				}
 274: 			    }
 275: 			}
 276: 		    }
 277: 		}
 278: 	    }
 279: 	}
 280:     }
 281: } /* End of do3 */
 282: 
 283: 
 284: static void do4 (int a, int b, int c, int d, int code)
 285: {
 286:     double	a1, b1, c1, d1;
 287:     int		index;
 288:     char	line[100];
 289:     int		ma, mb, mc, md, mx, my, mz;
 290:     int		n1, n2, n3;
 291:     double	x, x1, y, y1, z, z1;
 292:     
 293:     for (ma=0; ma<nops1; ma++)
 294:     {
 295: 	if (nops1 > 2)
 296: 	{
 297: 	    printf ("Starting %sa%s\n", name1a[ma], name1b[ma]);
 298: 	}
 299: 	if (!f (a, ma, &a1)) continue;
 300: 	for (mb=0; mb<nops1; mb++)
 301: 	{
 302: 	    if (!f (b, mb, &b1)) continue;
 303: 	    for (mc=0; mc<nops1; mc++)
 304: 	    {
 305: 		if (!f (c, mc, &c1)) continue;
 306: 		for (md=0; md<nops1; md++)
 307: 		{
 308: 		    if (!f (d, md, &d1)) continue;
 309: 		    for (n1=0; n1<NOPS2; n1++)
 310: 		    {
 311: 			switch (code)
 312: 			{
 313: 			case 1 : if (!fn (a1, n1, b1, &x)) continue;	break;
 314: 			case 2 : if (!fn (a1, n1, b1, &x)) continue;	break;
 315: 			case 3 : if (!fn (b1, n1, c1, &x)) continue;	break;
 316: 			case 4 : if (!fn (b1, n1, c1, &x)) continue;	break;
 317: 			case 5 : if (!fn (c1, n1, d1, &x)) continue;	break;
 318: 			}
 319: 			for (mx=0; mx<nops1; mx++)
 320: 			{
 321: 			    if (!f (x, mx, &x1)) continue;
 322: 			    for (n2=0; n2<NOPS2; n2++)
 323: 			    {
 324: 				switch (code)
 325: 				{
 326: 				case 1 : if (!fn (x1, n2, c1, &y)) continue;	break;
 327: 				case 2 : if (!fn (c1, n2, d1, &y)) continue;	break;
 328: 				case 3 : if (!fn (x1, n2, d1, &y)) continue;	break;
 329: 				case 4 : if (!fn (a1, n2, x1, &y)) continue;	break;
 330: 				case 5 : if (!fn (b1, n2, x1, &y)) continue;	break;
 331: 				}
 332: 				for (my=0; my<nops1; my++)
 333: 				{
 334: 				    if (!f (y, my, &y1)) continue;
 335: 				    for (n3=0; n3<NOPS2; n3++)
 336: 				    {
 337: 					for (mz=0; mz<nops1; mz++)
 338: 					{
 339: 					    
 340: 					    if (code == 1)
 341: 					    {
 342: 						/* if (!fn (a1, n1, b1, &x)) continue; */
 343: 						/* if (!f (x, mx, &x1)) continue; */
 344: 						/* if (!fn (x1, n2, c1, &y)) continue; */
 345: 						/* if (!f (y, my, &y1)) continue; */
 346: 						if (!fn (y1, n3, d1, &z)) continue;
 347: 						if (!f (z, mz, &z1)) continue;
 348: 						if ((index = tryit (z1)) != 0)
 349: 						{
 350: 						    sprintf (line, "%s%s%s(%s(%s%d%s %c %s%d%s)%s "
 351: 							"%c %s%d%s)%s %c %s%d%s%s%s",
 352: 							name1a[mz], (mz == 0 ? "" : "["),
 353: 							name1a[my],
 354: 							name1a[mx],
 355: 							name1a[ma], a, name1b[ma],
 356: 							name2[n1],
 357: 							name1a[mb], b, name1b[mb],
 358: 							name1b[mx],
 359: 							name2[n2],
 360: 							name1a[mc], c, name1b[mc],
 361: 							name1b[my],
 362: 							name2[n3],
 363: 							name1a[md], d, name1b[md],
 364: 							(mz == 0 ? "" : "]"), name1b[mz]);
 365: 						    putit (index, line);
 366: 						}
 367: 					    }
 368: 					    else if (code == 2)
 369: 					    {
 370: 						/* if (!fn (a1, n1, b1, &x)) continue; */
 371: 						/* if (!f (x, mx, &x1)) continue; */
 372: 						/* if (!fn (c1, n2, d1, &y)) continue; */
 373: 						/* if (!f (y, my, &y1)) continue; */
 374: 						if (!fn (x1, n3, y1, &z)) continue;
 375: 						if (!f (z, mz, &z1)) continue;
 376: 						if ((index = tryit (z1)) != 0)
 377: 						{
 378: 						    sprintf (line, "%s%s%s(%s%d%s %c %s%d%s)%s %c "
 379: 							"%s(%s%d%s %c %s%d%s)%s%s%s",
 380: 							name1a[mz], (mz == 0 ? "" : "["),
 381: 							name1a[mx],
 382: 							name1a[ma], a, name1b[ma],
 383: 							name2[n1],
 384: 							name1a[mb], b, name1b[mb],
 385: 							name1b[mx],
 386: 							name2[n3],
 387: 							name1a[my],
 388: 							name1a[mc], c, name1b[mc],
 389: 							name2[n2],
 390: 							name1a[md], d, name1b[md],
 391: 							name1b[my],
 392: 							(mz == 0 ? "" : "]"), name1b[mz]);
 393: 						    putit (index, line);
 394: 						}
 395: 					    }
 396: 					    else if (code == 3)
 397: 					    {
 398: 						/* if (!fn (b1, n1, c1, &x)) continue; */
 399: 						/* if (!f (x, mx, &x1)) continue; */
 400: 						/* if (!fn (x1, n2, d1, &y)) continue; */
 401: 						/* if (!f (y, my, &y1)) continue; */
 402: 						if (!fn (a1, n3, y1, &z)) continue;
 403: 						if (!f (z, mz, &z1)) continue;
 404: 						if ((index = tryit (z1)) != 0)
 405: 						{
 406: 						    sprintf (line, "%s%s%s%d%s %c %s(%s(%s%d%s %c "
 407: 							"%s%d%s)%s %c %s%d)%s%s%s",
 408: 							name1a[mz], (mz == 0 ? "" : "["),
 409: 							name1a[ma], a, name1b[ma],
 410: 							name2[n3],
 411: 							name1a[my],
 412: 							name1a[mx],
 413: 							name1a[mb], b, name1b[mb],
 414: 							name2[n1],
 415: 							name1a[mc], c, name1b[mc],
 416: 							name1b[mx],
 417: 							name2[n2],
 418: 							name1a[md], d, name1b[md],
 419: 							name1b[my],
 420: 							(mz == 0 ? "" : "]"), name1b[mz]);
 421: 						    putit (index, line);
 422: 						}
 423: 					    }
 424: 					    else if (code == 4)
 425: 					    {
 426: 						/* if (!fn (b1, n1, c1, &x)) continue; */
 427: 						/* if (!f (x, mx, &x1)) continue; */
 428: 						/* if (!fn (a1, n2, x1, &y)) continue; */
 429: 						/* if (!f (y, my, &y1)) continue; */
 430: 						if (!fn (y1, n3, d1, &z)) continue;
 431: 						if (!f (z, mz, &z1)) continue;
 432: 						if ((index = tryit (z1)) != 0)
 433: 						{
 434: 						    sprintf (line, "%s%s%s(%s%d%s %c %s(%s%d%s %c "
 435: 							"%s%d%s)%s)%s %c %s%d%s%s%s",
 436: 							name1a[mz], (mz == 0 ? "" : "["),
 437: 							name1a[my],
 438: 							name1a[ma], a, name1b[ma],
 439: 							name2[n2],
 440: 							name1a[mx],
 441: 							name1a[mb], b,	name1b[mb],
 442: 							name2[n1],
 443: 							name1a[mc], c, name1b[mc],
 444: 							name1b[mx],
 445: 							name1b[my],
 446: 							name2[n3],
 447: 							name1a[md], d, name1b[md],
 448: 							(mz == 0 ? "" : "]"), name1b[mz]);
 449: 						    putit (index, line);
 450: 						}
 451: 					    }
 452: 					    else	/* code == 5 */
 453: 					    {
 454: 						/* if (!fn (c1, n1, d1, &x)) continue; */
 455: 						/* if (!f (x, mx, &x1)) continue; */
 456: 						/* if (!fn (b1, n2, x1, &y)) continue; */
 457: 						/* if (!f (y, my, &y1)) continue; */
 458: 						if (!fn (a1, n3, y1, &z)) continue;
 459: 						if (!f (z, mz, &z1)) continue;
 460: 						if ((index = tryit (z1)) != 0)
 461: 						{
 462: 						    sprintf (line, "%s%s%s%d%s %c %s(%s%d%s %c "
 463: 							"%s(%s%d%s %c %s%d%s)%s)%s%s",
 464: 							name1a[mz], (mz == 0 ? "" : "["),
 465: 							name1a[ma], a, name1b[ma],
 466: 							name2[n3],
 467: 							name1a[my],
 468: 							name1a[mb], b, name1b[mb],
 469: 							name2[n2],
 470: 							name1a[mx],
 471: 							name1a[mc], c, name1b[mc],
 472: 							name2[n1],
 473: 							name1a[md], d, name1b[md],
 474: 							name1b[mx],
 475: 							name1b[my],
 476: 							(mz == 0 ? "" : "]"), name1b[mz]);
 477: 						    putit (index, line);
 478: 						}
 479: 					    }
 480: 					  }
 481: 					}
 482: 				  }
 483: 				}
 484: 			  }
 485: 			}
 486: 		  }
 487: 		}
 488: 	  }
 489: 	}
 490: } /* End of do4 */
 491: 
 492: 
 493: static void doit (int *num, int numbers)
 494: {
 495:     switch (numbers)
 496:     {
 497:     case 1 :
 498: 	do1 (num[0]);
 499: 	break;
 500: 	
 501:     case 2 :
 502: 	do2 (num[0], num[1]);
 503: 	break;
 504: 	
 505:     case 3 :
 506: 	do3 (num[0], num[1], num[2], 1);
 507: 	do3 (num[0], num[1], num[2], 2);
 508: 	break;
 509: 	
 510:     case 4 :
 511: 	do4 (num[0], num[1], num[2], num[3], 1);
 512: 	do4 (num[0], num[1], num[2], num[3], 2);
 513: 	do4 (num[0], num[1], num[2], num[3], 3);
 514: 	do4 (num[0], num[1], num[2], num[3], 4);
 515: 	do4 (num[0], num[1], num[2], num[3], 5);
 516: 	break;
 517:     }
 518: } /* End of doit */
 519: 
 520: 
 521: int main ()
 522: {
 523:     int		comb;
 524:     char	*descrip;
 525:     int		i;
 526:     int		iter;
 527:     long	minutes;
 528:     int		nc;
 529:     int		ncombs;
 530:     int		num[100];
 531:     int		numbers;
 532:     FILE	*out;
 533:     char	outname[110];
 534:     long	seconds;
 535:     clock_t	startime;
 536:     char	year[100];
 537:     
 538:     printf ("\nEnter year -->");
 539:     gets (year);
 540:     nc = strlen (year);
 541:     if (nc < 1 || nc > 5) exit(0);
 542:     sprintf (outname, "%s.out", year);
 543:     
 544:     /* Make sure the number they typed is ok */
 545:     for (i=0; i<nc; i++)
 546:     {
 547: 	if (year[i] < '0' || year[i] > '9')
 548: 	{
 549: 	    fprintf (stderr, "\nInvalid year: %s\n", year);
 550: 	    exit(0);
 551: 	}
 552:     }
 553:     
 554:     out = fopen (outname, "w");
 555:     if (out == NULL)
 556:     {
 557: 	fprintf (stderr, "\nUnable to write to %s\n", outname);
 558: 	exit(0);
 559:     }
 560:     
 561:     startime = clock ();
 562:     
 563:     /* 4 times: no monops, just neg, just sqrt, just factorial */
 564:     name1a[0] = "";
 565:     name1b[0] = "";
 566:     moncode[0] = NONE;
 567:     for (iter=0; iter<=4; iter++)
 568:     {
 569: 	switch (iter)
 570: 	{
 571: 	case 0 :
 572: 	    nops1 = 1;
 573: 	    descrip = "(none)";
 574: 	    break;
 575: 	    
 576: 	case 1 :
 577: 	    printf ("\nNow trying : negative\n\n");
 578: 	    nops1 = 2;
 579: 	    name1a[1] = "-";
 580: 	    name1b[1] = "";
 581: 	    moncode[1] = NEG;
 582: 	    descrip = "(negative)";
 583: 	    break;
 584: 	    
 585: 	case 2 :
 586: 	    printf ("\nNow trying : factorial\n\n");
 587: 	    nops1 = 2;
 588: 	    name1a[1] = "";
 589: 	    name1b[1] = "!";
 590: 	    moncode[1] = FACT;
 591: 	    descrip = "(factorial)";
 592: 	    break;
 593: 	    
 594: 	case 3 :
 595: 	    printf ("\nNow trying : sqrt\n\n");
 596: 	    nops1 = 2;
 597: 	    name1a[1] = "sqrt";
 598: 	    name1b[1] = "";
 599: 	    moncode[1] = SQRT;
 600: 	    descrip = "(square root)";
 601: 	    break;
 602: 	    
 603: 	case 4 :
 604: 	    printf ("\nNow trying : neg, sqrt, fact\n\n");
 605: 	    nops1 = 5;	/* NEGSQRT, NEGFACT didn't help! */
 606: 	    descrip = "(all three)";
 607: 	    
 608: 	    name1a[1] = "-";
 609: 	    name1b[1] = "";
 610: 	    moncode[1] = NEG;
 611: 	    
 612: 	    name1a[2] = "sqrt";
 613: 	    name1b[2] = "";
 614: 	    moncode[2] = SQRT;
 615: 	    
 616: 	    name1a[3] = "";
 617: 	    name1b[3] = "!";
 618: 	    moncode[3] = FACT;
 619: 	    
 620: 	    name1a[4] = "sqrt(";
 621: 	    name1b[4] = ")!";
 622: 	    moncode[4] = FACTSQRT;
 623: 	    
 624: 	    /*
 625: 	    name1a[4] = "-sqrt";
 626: 	    name1b[4] = "";
 627: 	    moncode[4] = NEGSQRT;
 628: 	    
 629: 	     name1a[5] = "-";
 630: 	     name1b[5] = "!";
 631: 	     moncode[5] = NEGFACT;
 632: 	    */
 633: 	    
 634: 	    break;
 635: 	}
 636: 	
 637: 	/* Now try all the combinations 1-9-9-5, 1-9-95, 19-9-5, etc */
 638: 	ncombs = powers2[nc-1];		/* 2^(nc-1) */
 639: 	for (comb=0; comb<ncombs; comb++)
 640: 	{
 641: 	    /* Initialize num array to all zero */
 642: 	    for (i=0; i<nc; i++) num[i] = 0;
 643: 	    
 644: 	    numbers = 0;
 645: 	    num[0] = year[0] - '0';
 646: 	    for (i=1; i<nc; i++)
 647: 	    {
 648: 		if ((powers2[i-1] & comb) != 0) numbers++;
 649: 		num[numbers] = 10*num[numbers] + year[i] - '0';
 650: 	    }
 651: 	    numbers++;
 652: 	    
 653: 	    /* Print out current pattern */
 654: 	    printf ("Pattern #%d = %d", comb+1, num[0]);
 655: 	    for (i=1; i<numbers; i++)
 656: 	    {
 657: 		printf (" - %d", num[i]);
 658: 	    }
 659: 	    printf ("\n");
 660: 	    
 661: 	    doit (num, numbers);
 662: 	}
 663: 	
 664: 	numbers = 0;
 665: 	for (i=0; i<100; i++)
 666: 	{
 667: 	    if (answer[i] != NULL) numbers++;
 668: 	}
 669: 	seconds = (clock() - startime) / CLOCKS_PER_SEC;
 670: 	minutes = seconds / 60;
 671: 	printf ("\nGot %d of 100 so far (%ld:%2.2ld:%2.2ld)\n",
 672: 	    numbers, minutes/60, minutes%60, seconds%60);
 673: 	fprintf (out, "After pass #%d %-14s : Got %3d of 100 (%ld:%2.2ld:%2.2ld)\n",
 674: 	    iter+1, descrip, numbers, minutes/60, minutes%60, seconds%60);
 675: 	}
 676: 	fprintf (out, "\n");
 677: 	
 678: 	/* Print answers */
 679: 	for (i=0; i<50; i++)
 680: 	{
 681: 	    if (answer[i] == NULL) fprintf (out, "%3d   %*c", i+1, 35, ' ');
 682: 	    else fprintf (out, "%3d = %s%*c", i+1, answer[i], 35-strlen(answer[i]), ' ');
 683: 	    
 684: 	    if (answer[i+50] == NULL) fprintf (out, "%3d\n", i+51);
 685: 	    else fprintf (out, "%3d = %s\n", i+51, answer[i+50]);
 686: 	}
 687: 	
 688: 	fclose (out);
 689: 	printf ("\nWrote log to %s\n", outname);
 690: 	
 691: 	return 0;
 692: } /* End of main */
 693: 

1995.out - Sample output

   1: After pass #1 (none)         : Got  35 of 100 (0:00:00)
   2: After pass #2 (negative)     : Got  47 of 100 (0:00:00)
   3: After pass #3 (factorial)    : Got  53 of 100 (0:00:00)
   4: After pass #4 (square root)  : Got  85 of 100 (0:00:00)
   5: After pass #5 (all three)    : Got 100 of 100 (0:00:01)
   6: 
   7:   1 = 1 ^ 995                             51 = (19 * 9) - 5!
   8:   2 = (19 - 9) / 5                        52 = (19 * sqrt9) - 5
   9:   3 = -[((1 ^ 9) - 9) + 5]                53 = -[(1 - 9) - (9 * 5)]
  10:   4 = ((1 ^ 9) * 9) - 5                   54 = (1 * 9) + (9 * 5)
  11:   5 = (1 ^ 99) * 5                        55 = (1 + 9) + (9 * 5)
  12:   6 = (1 ^ 99) + 5                        56 = (1 + sqrt9) * (9 + 5)
  13:   7 = 1 + ((9 / 9) + 5)                   57 = -[((1 + sqrt(9)!) * 9) - 5!]
  14:   8 = sqrt[19 + (9 * 5)]                  58 = ((1 + sqrt(9)!) * 9) - 5
  15:   9 = sqrt(19 - sqrt9) + 5                59 = ((1 + sqrt9) ^ sqrt9) - 5
  16:  10 = (1 + (9 / 9)) * 5                   60 = (sqrt(1 * 9) + 9) * 5
  17:  11 = (19 - sqrt9) - 5                    61 = 1 + ((9 + sqrt9) * 5)
  18:  12 = -[((1 - 9) - 9) + 5]                62 = (19 * sqrt9) + 5
  19:  13 = ((1 * 9) + 9) - 5                   63 = (-19 * sqrt9) + 5!
  20:  14 = ((1 + 9) + 9) - 5                   64 = 19 + (9 * 5)
  21:  15 = (19 - 9) + 5                        65 = ((1 + 9) + sqrt9) * 5
  22:  16 = -[(1 - (9 * 9)) / 5]                66 = -[(sqrt((1 * 9))! * 9) - 5!]
  23:  17 = (19 + sqrt9) - 5                    67 = -[((1 - 9) * 9) + 5]
  24:  18 = ((1 + 9) * 9) / 5                   68 = ((1 + sqrt(9)!) * 9) + 5
  25:  19 = sqrt[19 ^ sqrt(9 - 5)]              69 = ((1 + sqrt9) ^ sqrt9) + 5
  26:  20 = (1 + 99) / 5                        70 = -[((1 - 9) - sqrt(9)!) * 5]
  27:  21 = (19 - sqrt9) + 5                    71 = -[(1 + sqrt9)! + -95]
  28:  22 = -[((1 - 9) - 9) - 5]                72 = (1 * 9) * (sqrt9 + 5)
  29:  23 = (19 + 9) - 5                        73 = 1 + (9 * (sqrt9 + 5))
  30:  24 = ((1 + 9) + 9) + 5                   74 = -[1 - ((9 + sqrt(9)!) * 5)]
  31:  25 = (1 ^ 9) + (9 - 5)!                  75 = -[1 - ((9 * 9) - 5)]
  32:  26 = -19 + (9 * 5)                       76 = 19 * (9 - 5)
  33:  27 = (19 + sqrt9) + 5                    77 = 1 + ((9 * 9) - 5)
  34:  28 = sqrt(1 + sqrt9) * (9 + 5)           78 = -[((1 + sqrt(9)!) * sqrt(9)!) - 5!]
  35:  29 = sqrt((19 - sqrt9))! + 5             79 = 199 - 5!
  36:  30 = ((1 * 9) - sqrt9) * 5               80 = (19 - sqrt9) * 5
  37:  31 = ((1 + sqrt9) * 9) - 5               81 = sqrt[(1 * 9) ^ (9 - 5)]
  38:  32 = (1 + (9 / 9)) ^ 5                   82 = 1 + sqrt(9 ^ (9 - 5))
  39:  33 = (19 + 9) + 5                        83 = -[1 + ((sqrt(9)! * sqrt(9)!) - 5)!]
  40:  34 = (1 + 9) + (9 - 5)!                  84 = -[(sqrt((1 * 9))! * sqrt(9)!) - 5!]
  41:  35 = -[(1 + 9) - (9 * 5)]                85 = ((1 + 9) * 9) - 5
  42:  36 = (1 * 9) * (9 - 5)                   86 = ((1 * 9) * 9) + 5
  43:  37 = (1 - 9) + (9 * 5)                   87 = (1 - 9) + 95
  44:  38 = 19 * sqrt(9 - 5)                    88 = (-1 - sqrt(9)!) + 95
  45:  39 = -[((1 * 9) * 9) - 5!]               89 = -[1 - ((9 + 9) * 5)]
  46:  40 = (1 + 9) * (9 - 5)                   90 = ((1 * 9) + 9) * 5
  47:  41 = ((1 + sqrt9) * 9) + 5               91 = 1 + ((9 + 9) * 5)
  48:  42 = sqrt(1 * 9) * (9 + 5)               92 = (-1 * sqrt9) + 95
  49:  43 = 19 + (9 - 5)!                       93 = (-1 + 99) + -5
  50:  44 = -[(1 ^ 9) - (9 * 5)]                94 = (1 * 99) - 5
  51:  45 = ((1 ^ 9) * 9) * 5                   95 = (1 ^ 9) * 95
  52:  46 = (1 ^ 9) + (9 * 5)                   96 = (1 ^ 9) + 95
  53:  47 = sqrt(1 + sqrt9) + (9 * 5)           97 = sqrt(1 + sqrt9) + 95
  54:  48 = ((1 - 9) * 9) + 5!                  98 = (1 * sqrt9) + 95
  55:  49 = (1 + sqrt9) + (9 * 5)               99 = (1 + sqrt9) + 95
  56:  50 = (19 - 9) * 5                       100 = sqrt[(1 + 9) ^ (9 - 5)]
  57: 
Email: steve@oharasteve.com