home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 15 / CD_ASCQ_15_070894.iso / vrac / tpchal_1.zip / DD_1.C < prev    next >
C/C++ Source or Header  |  1994-05-26  |  1KB  |  58 lines

  1. /*
  2.  * Function to find the "magic" 9 digit number, where every substring
  3.  * of the number (to the left) is divisible by the number of digits in
  4.  * that substring.
  5.  *
  6.  * Dave Dunfield - May 20, 1994 - Released to the Public Domain
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <setjmp.h>
  12.  
  13. jmp_buf env;
  14.  
  15. long RetVal;
  16. char digits[10];
  17.  
  18. void findnum(long num, int n)
  19. {
  20.       long next;
  21.       int i;
  22.  
  23.       if(n >= 10)                               /* This number qualifies */
  24.       {
  25.             RetVal = num;
  26.             longjmp(env, -1);
  27.       }
  28.       else for(i=1; i < 10; ++i)                /* Test digits 1-9 only  */
  29.       {
  30.             if(digits[i])                       /* Digit in use          */
  31.                   continue;
  32.             if(!((next = num * 10 + i) % n))    /* Divides - proceed     */
  33.             {
  34.                   digits[i] = (char)-1;         /* Mark digit as used    */
  35.                   findnum(next, n + 1);         /* Try sub-combinations  */
  36.                   digits[i] = 0;                /* Release digit         */
  37.             }
  38.       }
  39. }
  40.  
  41. main()
  42. {
  43. #ifdef TEST
  44.       int I;
  45.  
  46.       for (I = 0; I < 1000; ++I)
  47.       {
  48. #endif
  49.             memset(digits, 0, sizeof(digits));
  50.             if (0 == setjmp(env))
  51.                   findnum(0L, 1);
  52. #ifdef TEST
  53.       }
  54. #endif
  55.       printf("Numer = %ld\n", RetVal);
  56.       return 0;
  57. }
  58.