Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- magic_square_sum_24_v1.c
- Task:
- https://www.facebook.com/groups/1941400139499670/posts/2686570531649290/
- Put the numbers 4,5,6,7,8,9,10,11,12 in the squares
- (don't repeat the numbers, one number has to be in one square),
- such that three numbers added to get 24,
- this must get in rows, columns and diagonal (two senses).
- Sum of the 3 numbers in each row, column and diagonal should be 24.
- All numbers are different, from range 4 to 12.
- 24 24 indexes of array:
- x1 x2 x3 = 24 0 1 2
- x4 x5 x6 = 24 3 4 5
- x7 x8 x9 = 24 6 7 8
- " " "
- 24 24 24 24 24
- Algorithm with permutations.
- https://www.codesdope.com/blog/article/generating-permutations-of-all-elements-of-an-arra/
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- */
- #include <stdio.h>
- #define SUM 24
- int number_of_solutions=0;
- unsigned number_of_attempts=0;
- //function to print the array of n integers
- void print_array(int arr[], int n)
- {
- number_of_attempts++;
- if( arr[0] + arr[1] + arr[2] == SUM &&
- arr[3] + arr[4] + arr[5] == SUM &&
- arr[6] + arr[7] + arr[8] == SUM &&
- arr[0] + arr[3] + arr[6] == SUM &&
- arr[1] + arr[4] + arr[7] == SUM &&
- arr[2] + arr[5] + arr[8] == SUM &&
- arr[0] + arr[4] + arr[8] == SUM &&
- arr[2] + arr[4] + arr[6] == SUM )
- {
- number_of_solutions++; // number_of_solutions and display them
- printf("\n---%3d. solution: --------\n\n", number_of_solutions);
- printf(" %3d %3d %3d \n", arr[0], arr[1], arr[2] );
- printf(" %3d %3d %3d \n", arr[3], arr[4], arr[5] );
- printf(" %3d %3d %3d \n", arr[6], arr[7], arr[8] );
- }
- } // print_array()
- // function to swap the variables a and b
- void swap(int *a, int *b)
- {
- int temp;
- temp = *a;
- *a = *b;
- *b = temp;
- }
- // recursive function to generate permutation
- void permutation(int *arr, int start, int end)
- {
- int i;
- if(start==end)
- {
- print_array(arr, end+1); // this is one permutation
- return;
- }
- for( i=start; i<=end; i++ ) // fixing one first digit,
- {
- swap((arr+i), (arr+start)); // swapping numbers
- permutation(arr, start+1, end); // and calling permutation on the rest of the digits
- swap((arr+i), (arr+start)); // swapping numbers
- }
- } // permutation()
- int main(void)
- {
- int i;
- int arr[9]={4,5,6,7,8,9,10,11,12}; // array of 9 permutation elements
- permutation(arr, 0, 8); //calling recursive function to generate permutations
- printf("\n Number of attempts is 9! = %ld \n", number_of_attempts);
- printf("\n There are %d solutions. \n", number_of_solutions);
- return 0;
- } // main()
- /*
- --- 1. solution: --------
- 5 10 9
- 12 8 4
- 7 6 11
- --- 2. solution: --------
- 5 12 7
- 10 8 6
- 9 4 11
- --- 3. solution: --------
- 7 6 11
- 12 8 4
- 5 10 9
- --- 4. solution: --------
- 7 12 5
- 6 8 10
- 11 4 9
- --- 5. solution: --------
- 9 4 11
- 10 8 6
- 5 12 7
- --- 6. solution: --------
- 9 10 5
- 4 8 12
- 11 6 7
- --- 7. solution: --------
- 11 6 7
- 4 8 12
- 9 10 5
- --- 8. solution: --------
- 11 4 9
- 6 8 10
- 7 12 5
- Number of attempts is 9! = 362880
- There are 8 solutions.
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement