Magic squares of doubly even order: Difference between revisions
Content deleted Content added
Line 1,504: | Line 1,504: | ||
The magic number is 870</pre> |
The magic number is 870</pre> |
||
=={{header|Phix}}== |
|||
{{trans|C++}} |
|||
<lang Phix>constant t = {{1,0,0,1}, |
|||
{0,1,1,0}, |
|||
{0,1,1,0}, |
|||
{1,0,0,1}} |
|||
function magic_square(integer n) |
|||
if n<4 or mod(n,4)!=0 then return false end if |
|||
sequence square = repeat(repeat(0,n),n) |
|||
integer i = 0 |
|||
for r=1 to n do |
|||
for c=1 to n do |
|||
square[r,c] = iff(t[mod(r-1,4)+1,mod(c-1,4)+1]?i+1:n*n-i) |
|||
i += 1 |
|||
end for |
|||
end for |
|||
return square |
|||
end function |
|||
procedure check(sequence sq) |
|||
integer n = length(sq) |
|||
integer magic = n*(n*n+1)/2 |
|||
integer bd = 0, fd = 0 |
|||
for i=1 to length(sq) do |
|||
if sum(sq[i])!=magic then ?9/0 end if |
|||
if sum(columnize(sq,i))!=magic then ?9/0 end if |
|||
bd += sq[i,i] |
|||
fd += sq[n-i+1,n-i+1] |
|||
end for |
|||
if bd!=magic or fd!=magic then ?9/0 end if |
|||
end procedure |
|||
--for i=4 to 16 by 4 do |
|||
for i=8 to 8 by 4 do |
|||
sequence square = magic_square(i) |
|||
printf(1,"maqic square of order %d, sum: %d\n", {i,sum(square[i])}) |
|||
string fmt = sprintf("%%%dd",length(sprintf("%d",i*i))) |
|||
pp(square,{pp_Nest,1,pp_IntFmt,fmt,pp_StrFmt,1,pp_Pause,0}) |
|||
check(square) |
|||
end for</lang> |
|||
{{out}} |
|||
<pre> |
|||
maqic square of order 8, sum: 260 |
|||
{{ 1,63,62, 4, 5,59,58, 8}, |
|||
{56,10,11,53,52,14,15,49}, |
|||
{48,18,19,45,44,22,23,41}, |
|||
{25,39,38,28,29,35,34,32}, |
|||
{33,31,30,36,37,27,26,40}, |
|||
{24,42,43,21,20,46,47,17}, |
|||
{16,50,51,13,12,54,55, 9}, |
|||
{57, 7, 6,60,61, 3, 2,64}} |
|||
</pre> |
|||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |