Magic Squares

 

 

            Suppose there is a NxN matrix. We want to fill the matrix with numbers from 1 to N*N( only once is used) and every row's/column's total is same. 

Here is an example. N=5

 

1

7

13

19

25

65

14

20

21

2

8

65

22

3

9

15

16

65

10

11

17

23

4

65

18

24

5

6

12

65

65

65

65

65

65

 

 

 

            It's really hard to write a program for any number N but if we can except the even numbers and take only odd numbers we can write a program to generate such matrices.

 

 

 

The Algorithm

 

 

 

 

 

 

 

 

 

  

 


C Source Code

 

#include<stdio.h>
 
int main(void)
{
    int i, j, n, k1, k2, z1, z2, Dizi[10000], A[30][30], B[30][30], D[30][30];

    printf("Enter the number (below 30 and odd): ");scanf("%d",&n);
    if ((n&1) == 0)
    {
       printf("ERROR! Must be ODD number");
       return 0;
    }
    for(i=0;i<n;i++)
    {
       Dizi[i] = i;
    }
    z1 = k1 = n/2 + 1 - 1;
    z2 = k2 = n/2 + 2 - 1;
    for(i=0;i<n;i++)
    {
       A[0][i] = i;
       B[0][i] = i;
    }
    for(i=1;i<n;i++)
    {
       for(j=0;j<n;j++)
       {
          A[i][j] = Dizi[z1];
          B[i][j] = Dizi[z2];
          z1++;
          z2++;
          if(z1>=n)
          {
                      z1 = 0;
          }
          if(z2>=n)
          {
                      z2 = 0;
          }
       }
       z1 = A[i][k1];
       z2 = B[i][k2];
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            D[i][j] = n * (A[i][j]+1 - 1) + B[i][j]+1;
        }
    }
    printf("Magic Square:\n");
    for(i=0;i<n;i++)
    {
       for(j=0;j<n;j++)
       {
          printf("%3d ", D[i][j]);
       }
       z1 = 0;
       for(j=0;j<n;j++) 
          z1 += D[i][j];
       printf("= [%d]\n", z1);
    }
    printf("+__");
    for(i=1;i<n;i++)
                printf("___");
    printf("\n");
    for(j=0;j<n;j++)
    {
       z1 = 0;
       for(i=0;i<n;i++)
                  z1 += D[i][j];
       printf("%3d ", z1);
    }
 
    return 0;
}
 

 

 

Sample Execution

 

 

 

Enter the number (below 30 and odd): 5

Magic Square:

 

1

7

13

19

25

65

14

20

21

2

8

65

22

3

9

15

16

65

10

11

17

23

4

65

18

24

5

6

12

65

65

65

65

65

65

 

 

------------------------------------------------------------------------------------

 

Enter the number (below 30 and odd): 7

Magic Square:

 

1

9

17

25

33

41

49

175

26

34

42

43

2

10

18

175

44

3

11

19

27

35

36

175

20

28

29

37

45

4

12

175

38

46

5

13

21

22

30

175

14

15

23

31

39

47

6

175

32

40

48

7

8

16

24

175

175

175

175

175

175

175

175

 

 

------------------------------------------------------------------------------------

 

Enter the number (below 30 and odd): 11

Magic Square:

 

1

13

25

37

49

61

73

85

97

109

121

671

62

74

86

98

110

111

2

14

26

38

50

671

112

3

15

27

39

51

63

75

87

99

100

671

52

64

76

88

89

101

113

4

16

28

40

671

102

114

5

17

29

41

53

65

77

78

90

671

42

54

66

67

79

91

103

115

6

18

30

671

92

104

116

7

19

31

43

55

56

68

80

671

32

44

45

57

69

81

93

105

117

8

20

671

82

94

106

118

9

21

33

34

46

58

70

671

22

23

35

47

59

71

83

95

107

119

10

671

72

84

96

108

120

11

12

24

36

48

60

671

671

671

671

671

671

671

671

671

671

671

671

 

 
Copyright by Chasan Chouse