dmilicev

nine-five-four_zero.c

Oct 24th, 2021 (edited)
268
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3.     nine-five-four_zero.c
  4.  
  5.     Task from Graham Holmes:
  6.     https://www.facebook.com/groups/242739573272753/permalink/874176896795681/
  7.  
  8.     NINE - FIVE - FOUR = ZERO
  9.  
  10.     Replace each letter with a digit from 0 to 9 to make this sum add up.
  11.     No two letters can have the same digit,
  12.     and no two digits can share the same letter.
  13.     Leading zeroes aren't permitted.
  14.     What is the value of ZERO?
  15.  
  16.  Result:
  17.  Value of ZERO is 1564.
  18.  
  19.  There are 4 solutions:
  20.  
  21.  NINE - FIVE - FOUR = ZERO
  22.  8085 - 3025 - 3496 = 1564
  23.  
  24.  NINE - FIVE - FOUR = ZERO
  25.  8085 - 3095 - 3426 = 1564
  26.  
  27.  NINE - FIVE - FOUR = ZERO
  28.  8785 - 3725 - 3496 = 1564
  29.  
  30.  NINE - FIVE - FOUR = ZERO
  31.  8785 - 3795 - 3426 = 1564
  32.  
  33.  
  34.     You can find all my C programs at Dragan Milicev's pastebin:
  35.  
  36.     https://pastebin.com/u/dmilicev
  37.  
  38. */
  39.  
  40. #include <stdio.h>
  41.  
  42. // Returns 1 if all digits are different from each other, otherwise returns 0.
  43. int allDifferent(int n, int i, int e, int f, int v, int o, int u, int r, int z)
  44. {
  45.     int arr[9]={n,i,e,f,v,o,u,r,z};
  46.     int len = sizeof(arr) / sizeof(arr[0]);
  47.     int x, y;
  48.  
  49.     for(x=0; x<len; x++)
  50.       for(y=x+1; y<len; y++)
  51.         if( arr[x] == arr[y] )
  52.             return 0;
  53.  
  54.     return 1;
  55. }
  56.  
  57. int main(void)
  58. {
  59.     int n, i, e, f, v, o, u, r, z, nine, five, four, zero, solved=0, number_of_solutions=0;
  60.  
  61.     for(n=1; n<10; n++)
  62.       for(i=0; i<10; i++)
  63.         for(e=0; e<10; e++)
  64.           for(f=1; f<10; f++)
  65.             for(v=0; v<10; v++)
  66.               for(o=0; o<10; o++)
  67.                 for(u=0; u<10; u++)
  68.                   for(r=0; r<10; r++)
  69.                     for(z=1; z<10; z++)
  70.                     {
  71.                       nine = n*1000 + i*100 + n*10 + e;
  72.                       five = f*1000 + i*100 + v*10 + e;
  73.                       four = f*1000 + o*100 + u*10 + r;
  74.                       zero = z*1000 + e*100 + r*10 + o;
  75.  
  76.                       if( nine - five - four == zero  &&
  77.                           allDifferent(n,i,e,f,v,o,u,r,z) )
  78.                       {
  79.                         solved = 1;
  80.                         number_of_solutions++;
  81.                         printf("\n NINE - FIVE - FOUR = ZERO \n");
  82.                         printf("\n %d%d%d%d - %d%d%d%d - %d%d%d%d = %d%d%d%d \n\n",
  83.                                 n,i,n,e, f,i,v,e, f,o,u,r, z,e,r,o );
  84.                       }
  85.                     }
  86.  
  87.     if( !solved )
  88.         printf("\n There is no solution. \n");
  89.     else
  90.         printf("\n There are %d solutions. \n", number_of_solutions);
  91.  
  92.  
  93.     return 0;
  94.  
  95. } // main()
  96.  
RAW Paste Data Copied