Advertisement
dmilicev

magic_square_sum_15_v1.c

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