How to create a magic square if we know a date. Eg-22-04-2014 The first column should have 22 2nd-04 3rd-20 and 4th -14. I believe ramanujan created the same thing for his birthday but I don't know the method he used. Please help. Is there any method to do it? I tried doing it using the fact that the row diagonal 2*2 square sum should all be equal to the total sum of the first row in this case 22+4+20+14=60
Magic square of a given date
1.1k Views Asked by Bumbble Comm https://math.techqa.club/user/bumbble-comm/detail AtThere are 2 best solutions below
On
Let the magic square be $\begin{bmatrix}a & b & c & d \\ x_1 & x_2 & x_3 & x_4 \\ x_5 & x_6 & x_7 & x_8 \\ x_9 & x_{10} & x_{11} & x_{12}\end{bmatrix}$ where $a,b,c,d$ represents the date.
The conditions that the rows, columns, and diagonals sum to $a+b+c+d$ gives us $9$ linear equations for $12$ variables. The resulting system has rank $8$.
Therefore, any solution can be obtained by taking a particular solution and adding a linear combination of $12-8 = 4$ "basis vectors" for the nullspace of the system.
The matrix $\begin{bmatrix}a & b & c & d \\ c & d & a & b \\ d & c & b & a \\ b & a & d & c\end{bmatrix}$ is a magic square with the correct top row.
The following $4$ matrices form a basis of the nullspace of the system (all rows, cols, diags sum to $0$):
$\begin{bmatrix}0 & 0 & 0 & 0\\ 2 & -1 & -1 & 0 \\ -2 & 1 & 1 & 0 \\ 0 & 0 & 0 & 0\end{bmatrix}$, $\begin{bmatrix}0 & 0 & 0 & 0\\ 1 & 0 & 0 & -1 \\ -1 & 0 & 0 & 1 \\ 0 & 0 & 0 & 0\end{bmatrix}$, $\begin{bmatrix}0 & 0 & 0 & 0\\ 1 & 0 & -1 & 0 \\ -1 & 1 & 0 & 0 \\ 0 & -1 & 1 & 0\end{bmatrix}$, $\begin{bmatrix}0 & 0 & 0 & 0\\ 2 & -1 & 0 & -1 \\ -1 & 1 & 0 & 0 \\ -1 & 0 & 0 & 1\end{bmatrix}$.
Take the first matrix and add any linear combination of the above four matrices to get a solution.
Here is a solution in Perl, which makes no claim to optimization but does produce results. The algorithm uses a mixing strategy to obtain balanced squares. For today the fourteenth of August 2014 we get the following squares (initial segment shown)
For Xmas eve we obtain
Euler's birthday was 15-04-1707:
Gauss's birthday was 30-04-1777:
This is the code which it may be a useful exercise to study and improve. As I mentioned it really does admit improvement in several spots but demonstrates proof of concept.
#! /usr/bin/perl -w # sub search { my ($sq, $sum, $sofar, $seen) = @_; if($sofar == 16){ for(my $row=0; $row<4; $row++){ for(my $col=0; $col<4; $col++){ printf "%03d ", $sq->[$row][$col]; } print "\n"; } print "\n"; return; } my $loc_col = $sofar % 4; my $loc_row = ($sofar - $loc_col) / 4; my $tries = [(1..$sum)]; for(my $pos=$sum-1; $pos>=0; $pos--){ my $targ = int rand($pos+1); my $tmp; $tmp = $tries->[$targ]; $tries->[$targ] = $tries->[$pos]; $tries->[$pos] = $tmp; } my $ind = 0; while($ind < scalar(@$tries)){ my $nxt = $tries->[$ind]; if(!exists($seen->{$nxt})){ $seen->{$nxt} = 1; $sq->[$loc_row][$loc_col] = $nxt; my $no_admit = undef; my ($empty, $sval); for(my $row=0; $row<4; $row++){ $empty = 0; $sval = 0; for(my $col=0; $col<4; $col++){ if($sq->[$row][$col] == -1){ $empty++; } else{ $sval += $sq->[$row][$col]; } } if(($empty == 0 && $sval != $sum) || ($empty > 0 && $sval >= $sum)){ $no_admit = 1; } } for(my $col=0; $col<4; $col++){ $empty = 0; $sval = 0; for(my $row=0; $row<4; $row++){ if($sq->[$row][$col] == -1){ $empty++; } else{ $sval += $sq->[$row][$col]; } } if(($empty == 0 && $sval != $sum) || ($empty > 0 && $sval >= $sum)){ $no_admit = 1; } } $empty = 0; $sval = 0; for(my $diag=0; $diag<4; $diag++){ if($sq->[$diag][$diag] == -1){ $empty++; } else{ $sval += $sq->[$diag][$diag]; } } if(($empty == 0 && $sval != $sum) || ($empty > 0 && $sval >= $sum)){ $no_admit = 1; } $empty = 0; $sval = 0; for(my $diag=0; $diag<4; $diag++){ if($sq->[$diag][3-$diag] == -1){ $empty++; } else{ $sval += $sq->[$diag][3-$diag]; } } if(($empty == 0 && $sval != $sum) || ($empty > 0 && $sval >= $sum)){ $no_admit = 1; } if(!defined($no_admit)){ search($sq, $sum, $sofar+1, $seen); } $sq->[$loc_row][$loc_col] = -1; delete $seen->{$nxt}; } $ind++; } } MAIN: { my $date = shift || '31-12-2014'; die "not a date" if $date !~ /^(\d\d)-(\d\d)-(\d\d)(\d\d)$/; my $first = [$1, $2, $3, $4]; my $sum = $first->[0]+$first->[1] +$first->[2]+$first->[3]; print "$sum\n"; my $sq = []; push @$sq, $first; for(my $row=1; $row<4; $row++){ push @$sq, [-1, -1, -1, -1]; } my $seen = { $first->[0] => 1, $first->[1] => 1, $first->[2] => 1, $first->[3] => 1 }; search($sq, $sum, 4, $seen); }