-
Notifications
You must be signed in to change notification settings - Fork 0
/
magic_v1.c
70 lines (64 loc) · 1.68 KB
/
magic_v1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int count = 0;
int check(int v[]) {
int i, j;
int s[16];
s[0] = -34 + v[0] + v[3] + v[4] + v[5] + v[6];
s[1] = -34 + v[0] - v[1] + v[2] + v[3] + v[5] + 2 * v[6];
s[2] = 68 - v[0] + v[1] - v[2] - v[3] - v[4] - 2 * v[5] - 2 * v[6];
s[3] = 34 - v[0] - v[3] - v[6];
s[4] = 0 - v[0] + v[1] + v[2];
s[5] = 68 - v[0] - v[2] - v[3] - v[4] - v[5] - 2 * v[6];
s[6] = -34 + v[0] - v[1] + v[3] + v[4] + v[5] + 2 * v[6];
s[7] = v[0];
s[8] = 34 - v[1] - v[2] - v[3];
s[9] = v[1];
s[10] = v[2];
s[11] = v[3];
s[12] = 34 - v[4] - v[5] - v[6];
s[13] = v[4];
s[14] = v[5];
s[15] = v[6];
for (i = 0; i < 16; i++) {
if (s[i] < 1 || s[i] > 16) {
return 0;
}
for (j = i + 1; j < 16; j++) {
if (s[i] == s[j]) {
return 0;
}
}
}
return 1;
}
void find(int square[], int entry) {
int i, j;
if (entry == 7) {
if (check(square)) {
count++;
}
return;
}
for (i = 1; i <= 16; i++) {
for (j = 0; j < entry; j++) {
if (square[j] == i) {
goto A;
}
}
square[entry] = i;
find(square, entry + 1);
A: i=i;
}
}
void main() {
clock_t prgstart, prgende;
prgstart=clock();
int* start = (int*) malloc(7*sizeof(int));
find(start, 0);
free(start);
prgende=clock();
printf("There are %i magic squares.\n", count);
printf("Runtime %.2f seconds\n",(float)(prgende-prgstart) / CLOCKS_PER_SEC);
}