I wish to post an example here, I just wrote my Magic Square for two languages, first I wrote in C++, then a basic editing, and the program is a python program!

Modern languages are so identical, except their specific APIs/fields of work, etc, that language choice hardly matters!

Magic Square:

C++ version:

Code:

#include <iostream>
#include <vector>
void magic(int n)
{
std::cout << "Magic Square: " << n << "x" << n << std::endl;
std::vector < std::vector <int> > grid;
for (int i=1; i<=n; i++) grid.push_back(std::vector<int>(n, 0));
int row = 0, col = n/2;
int n2 = n*n, v = 1;
int r, c;
grid[row][col] = v;
while (v != n2) {
v++;
if ((row-1) >= 0) r = row-1;
else r = n-1;
if ((col+1) < n) c = col+1;
else c = 0;
if (grid[r][c]) {
if ((row+1) < n) {
r = row+1;
if (grid[r][col]) break;
c = col;
}
}
grid[r][c] = v;
row = r;
col = c;
}
for (r=0; r<n; r++) {
for (c=0; c<n; c++) {
if (grid[r][c] < 10) std::cout << " ";
std::cout << grid[r][c] << " ";
}
std::cout << std::endl;
}
}
int main(int argc, char *argv[])
{
if (argc > 1) {
while (--argc) {
int n = atoi(*++argv);
if (n % 2 == 0) {
std::cout << "Only odd size allowed for Magic Square: "
<< n << std::endl;
} else {
magic(n);
}
}
} else {
magic(3);
}
return 0;
}

Python version:

Code:

import sys
def magic(n):
print "Magic Square: %dx%d" % (n, n)
grid = [ [ 0 for c in xrange(n) ] for r in xrange(n) ]
row, col = 0, n/2
n2, v = n*n, 1;
r, c = 0, 0
grid[row][col] = v
while v != n2:
v += 1
if (row-1) >= 0: r = row-1
else: r = n-1
if (col+1) < n: c = col+1
else: c = 0
if grid[r][c]:
if (row+1) < n:
r = row+1
if grid[r][col]: break
c = col
grid[r][c] = v
row = r
col = c
for r in xrange(n):
for c in xrange(n):
print "%2d" % grid[r][c],
print
if len(sys.argv) > 1:
for arg in sys.argv[1:]:
n = int(arg)
if n % 2 == 0:
print "Only odd size allowed for Magic Square:", n
else:
magic(n)
else:
magic(3)
sys.exit(0)