Y bueno, tengo algo de tiempo libre, a ver si jala esto:
Código Delphi
[-]
type
TMatriz = array of array of Integer;
function CuadradoMagico(N: Integer): TMatriz;
var
D, I, J: Integer;
begin
if not Odd(N)then
raise Exception.CreateFmt('%d no es un número impar', [N]);
SetLength(Result, N, N);
for I := 0 to N - 1 do
for J := 0 to N - 1 do
Result[I, J] := 0;
I := 0; J := (N - 1) div 2;
for D := 1 to N*N do
begin
Result[I, J] := D;
if Result[(((I - 1) mod N) + N) mod N, (J + 1) mod N] <> 0 then
Inc(I)
else
begin
I := (((I - 1) mod N) + N) mod N;
J := (J + 1) mod N;
end;
end;
end;
Ejemplo de uso, usando un StringGrid para mostrar los resultados:
Código Delphi
[-]
var
N, I, J: Integer;
M: TMatriz;
begin
N := StrToIntDef(Edit1.Text, 3);
M := CuadradoMagico(N);
StringGrid1.RowCount := N;
StringGrid1.ColCount := N;
for I := 0 to N - 1 do
for J := 0 to N - 1 do
StringGrid1.Cells[J, I] := IntToStr(M[I, J]);
end;
// Saludos