Advertisement
dmilicev

magic_square_multiplication_v1.c

Apr 30th, 2020
1,341
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.88 KB | None
  1. /*
  2.  
  3.     magic_square_multiplication_v1.c
  4.  
  5.     Product of the 3 numbers in each row and column.
  6.     All numbers are different, from range 1 to 9.
  7.  
  8.     x5 = 5 .
  9.  
  10.                                         indexes of array:
  11.  
  12.         x1 x x2 x x3  =   54              0   1   2
  13.          x    x    x
  14.         x4 x x5 x x6  =   120             3   4   5
  15.          x    x    x
  16.         x7 x x8 x x9  =   56              6   7   8
  17.  
  18.         "   "   "
  19.         96 180  21
  20.  
  21. https://web.facebook.com/photo.php?fbid=545662472810656&set=a.102725373771037&type=3&theater
  22.  
  23.     Algorithm with permutations.
  24.  
  25.     https://www.codesdope.com/blog/article/generating-permutations-of-all-elements-of-an-arra/
  26.  
  27.  
  28.     You can find all my C programs at Dragan Milicev's pastebin:
  29.  
  30.     https://pastebin.com/u/dmilicev
  31.  
  32. */
  33.  
  34. #include <stdio.h>
  35.  
  36. int number_of_solutions=0;
  37. unsigned number_of_attempts=0;
  38.  
  39. //function to print the array of n integers
  40. void print_array(int arr[], int n)
  41. {
  42.     number_of_attempts++;
  43.  
  44.     if( arr[0] * arr[1] * arr[2] ==  54 &&
  45.         arr[3] * arr[4] * arr[5] == 120 &&
  46.         arr[6] * arr[7] * arr[8] ==  56 &&
  47.         arr[0] * arr[3] * arr[6] ==  96 &&
  48.         arr[1] * arr[4] * arr[7] == 180 &&
  49.         arr[2] * arr[5] * arr[8] ==  21     )
  50.     {
  51.             number_of_solutions++;  // number_of_solutions and display them
  52.             printf("\n---%3d. solution: --------\n\n", number_of_solutions);
  53.             printf(" %3d %3d %3d \n", arr[0], arr[1], arr[2] );
  54.             printf(" %3d %3d %3d \n", arr[3], arr[4], arr[5] );
  55.             printf(" %3d %3d %3d \n", arr[6], arr[7], arr[8] );
  56.     }
  57. } // print_array()
  58.  
  59. // function to swap the variables a and b
  60. void swap(int *a, int *b)
  61. {
  62.     int temp;
  63.     temp = *a;
  64.     *a = *b;
  65.     *b = temp;
  66. }
  67.  
  68. // recursive function to generate permutation
  69. void permutation(int *arr, int start, int end)
  70. {
  71.     int i;
  72.  
  73.     if(start==end)
  74.     {
  75.         print_array(arr, end+1);        // this is one permutation
  76.         return;
  77.     }
  78.  
  79.     for( i=start; i<=end; i++ )         // fixing one first digit,
  80.     {
  81.         swap((arr+i), (arr+start));     // swapping numbers
  82.         permutation(arr, start+1, end); // and calling permutation on the rest of the digits
  83.         swap((arr+i), (arr+start));     // swapping numbers
  84.     }
  85. } // permutation()
  86.  
  87.  
  88. int main(void)
  89. {
  90.     int i;
  91.  
  92.     int arr[9];                 // array of 9 permutation elements
  93.  
  94.     for (i = 0; i < 9; i++)     // fill in the array with integers from 1 to 9
  95.         arr[i] = i + 1;
  96.  
  97.     permutation(arr, 0, 8);     //calling recursive function to generate permutations
  98.  
  99.     printf("\n Number of attempts is 9! = %ld \n", number_of_attempts);
  100.     printf("\n There are %d solutions. \n", number_of_solutions);
  101.  
  102.     return 0;
  103. } // main()
  104.  
  105. /*
  106.  
  107. ---  1. solution: --------
  108.  
  109.    6   9   1
  110.    8   5   3
  111.    2   4   7
  112.  
  113.  Number of attempts is 9! = 362880
  114.  
  115.  There are 1 solutions.
  116.  
  117. */
Advertisement
RAW Paste Data Copied
Advertisement