Advertisement
dmilicev

magic_square_sum_24_v1.c

Nov 19th, 2021
638
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3.     magic_square_sum_24_v1.c
  4.  
  5.     Task:
  6.     https://www.facebook.com/groups/1941400139499670/posts/2686570531649290/
  7.  
  8.     Put the numbers 4,5,6,7,8,9,10,11,12 in the squares
  9.     (don't repeat the numbers, one number has to be in one square),
  10.     such that three numbers added to get 24,
  11.     this must get in rows, columns and diagonal (two senses).
  12.  
  13.     Sum of the 3 numbers in each row, column and diagonal should be 24.
  14.     All numbers are different, from range 4 to 12.
  15.  
  16.     24                24                indexes of array:
  17.  
  18.         x1  x2  x3  =   24              0   1   2
  19.  
  20.         x4  x5  x6  =   24              3   4   5
  21.  
  22.         x7  x8  x9  =   24              6   7   8
  23.  
  24.         "   "   "
  25.   24    24  24  24     24
  26.  
  27.  
  28.     Algorithm with permutations.
  29.  
  30.     https://www.codesdope.com/blog/article/generating-permutations-of-all-elements-of-an-arra/
  31.  
  32.  
  33.     You can find all my C programs at Dragan Milicev's pastebin:
  34.  
  35.     https://pastebin.com/u/dmilicev
  36.  
  37. */
  38.  
  39. #include <stdio.h>
  40.  
  41. #define SUM 24
  42.  
  43. int number_of_solutions=0;
  44. unsigned number_of_attempts=0;
  45.  
  46. //function to print the array of n integers
  47. void print_array(int arr[], int n)
  48. {
  49.     number_of_attempts++;
  50.  
  51.     if( arr[0] + arr[1] + arr[2] == SUM &&
  52.         arr[3] + arr[4] + arr[5] == SUM &&
  53.         arr[6] + arr[7] + arr[8] == SUM &&
  54.         arr[0] + arr[3] + arr[6] == SUM &&
  55.         arr[1] + arr[4] + arr[7] == SUM &&
  56.         arr[2] + arr[5] + arr[8] == SUM &&
  57.         arr[0] + arr[4] + arr[8] == SUM &&
  58.         arr[2] + arr[4] + arr[6] == SUM     )
  59.     {
  60.             number_of_solutions++;  // number_of_solutions and display them
  61.             printf("\n---%3d. solution: --------\n\n", number_of_solutions);
  62.             printf(" %3d %3d %3d \n", arr[0], arr[1], arr[2] );
  63.             printf(" %3d %3d %3d \n", arr[3], arr[4], arr[5] );
  64.             printf(" %3d %3d %3d \n", arr[6], arr[7], arr[8] );
  65.     }
  66. } // print_array()
  67.  
  68. // function to swap the variables a and b
  69. void swap(int *a, int *b)
  70. {
  71.     int temp;
  72.     temp = *a;
  73.     *a = *b;
  74.     *b = temp;
  75. }
  76.  
  77. // recursive function to generate permutation
  78. void permutation(int *arr, int start, int end)
  79. {
  80.     int i;
  81.  
  82.     if(start==end)
  83.     {
  84.         print_array(arr, end+1);        // this is one permutation
  85.         return;
  86.     }
  87.  
  88.     for( i=start; i<=end; i++ )         // fixing one first digit,
  89.     {
  90.         swap((arr+i), (arr+start));     // swapping numbers
  91.         permutation(arr, start+1, end); // and calling permutation on the rest of the digits
  92.         swap((arr+i), (arr+start));     // swapping numbers
  93.     }
  94. } // permutation()
  95.  
  96.  
  97. int main(void)
  98. {
  99.     int i;
  100.  
  101.     int arr[9]={4,5,6,7,8,9,10,11,12};  // array of 9 permutation elements
  102.  
  103.     permutation(arr, 0, 8);     //calling recursive function to generate permutations
  104.  
  105.     printf("\n Number of attempts is 9! = %ld \n", number_of_attempts);
  106.     printf("\n There are %d solutions. \n", number_of_solutions);
  107.  
  108.     return 0;
  109. } // main()
  110.  
  111. /*
  112.  
  113. ---  1. solution: --------
  114.  
  115.    5  10   9
  116.   12   8   4
  117.    7   6  11
  118.  
  119. ---  2. solution: --------
  120.  
  121.    5  12   7
  122.   10   8   6
  123.    9   4  11
  124.  
  125. ---  3. solution: --------
  126.  
  127.    7   6  11
  128.   12   8   4
  129.    5  10   9
  130.  
  131. ---  4. solution: --------
  132.  
  133.    7  12   5
  134.    6   8  10
  135.   11   4   9
  136.  
  137. ---  5. solution: --------
  138.  
  139.    9   4  11
  140.   10   8   6
  141.    5  12   7
  142.  
  143. ---  6. solution: --------
  144.  
  145.    9  10   5
  146.    4   8  12
  147.   11   6   7
  148.  
  149. ---  7. solution: --------
  150.  
  151.   11   6   7
  152.    4   8  12
  153.    9  10   5
  154.  
  155. ---  8. solution: --------
  156.  
  157.   11   4   9
  158.    6   8  10
  159.    7  12   5
  160.  
  161.  Number of attempts is 9! = 362880
  162.  
  163.  There are 8 solutions.
  164.  
  165. */
  166.  
Advertisement
RAW Paste Data Copied
Advertisement