I watched Sagrada Familia on a TV program. It is quite rare for the interior of the church to be broadcast on television, and I was astonished by its beauty. It has become one of the places I would like to visit someday. As usual, I used Wolfram Language to explore some aspects of the magic square that were introduced in it.
What is the magic square on Sagrada Familia
What is the magic square on Sagrada Familia
The magic square on the Sagrada Familia’s Passion facade, designed by Josep Maria Subirachs, is a variation of the standard 4x4 magic square. A typical 4x4 magic square uses numbers 1 through 16, each appearing once, with a magic constant of 34. However, the magic square on Sagrada Familia uniquely uses the numbers 1 to 15, but repeats 10 and 14 while omitting 12 and 16. This results in a different magic constant of 33.This modification is believed to be intentional, possibly symbolizing the age of Jesus Christ at the time of the Passion.
=i
16
∑
i=1
4
In[]:=
sagradafamiliamagicsquarelist={1,14,14,4,11,7,6,9,8,10,10,5,13,2,3,15};Grid[Partition[sagradafamiliamagicsquarelist,4],Frame->All]
Out[]=
1 | 14 | 14 | 4 |
11 | 7 | 6 | 9 |
8 | 10 | 10 | 5 |
13 | 2 | 3 | 15 |
The magic constant is
In[]:=
sagradafamiliamagicconstant=Total@sagradafamiliamagicsquarelist/4
Out[]=
33
Solve the magic square puzzle
Solve the magic square puzzle
Solving a magic square fundamentally involves trying all possible number combinations (“brute force”) to see if they result in a correct solution. However, in this particular magic square, since we know that the sum of each row, column, diagonal, and the four corners must equal 33, once 3 cells in any of these groups are filled, the remaining cell can be automatically determined, and thus excluded from the brute-force approach.
This allows us to reduce the number of cells subject to brute force from 16 to 7. In other words, the number of combinations to be tried is drastically reduced from 5,230,697,472,000 to 24,060,960.
This allows us to reduce the number of cells subject to brute force from 16 to 7. In other words, the number of combinations to be tried is drastically reduced from 5,230,697,472,000 to 24,060,960.
Specifically:
1. Generate all subsets containing exactly 7 numbers(the light gray area in the figure below) from the set of 16 numbers.
2. For a given set of 7 numbers, determine the numbers for other 9 numbers in the order(16->13->10->3->14->15->9->8->12):
If there is no number that makes the sum of the 4 numbers equal to 33, move to the next set of 7 numbers.
1. Generate all subsets containing exactly 7 numbers(the light gray area in the figure below) from the set of 16 numbers.
2. For a given set of 7 numbers, determine the numbers for other 9 numbers in the order(16->13->10->3->14->15->9->8->12):
If there is no number that makes the sum of the 4 numbers equal to 33, move to the next set of 7 numbers.
Out[]=
1 | 2 | 3 | 4 |
5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 |
Generate all subsets containing exactly 7 numbers from the set of 16 numbers, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 11, 13, 14, 14, 15}
In[]:=
p7=Permutations[sagradafamiliamagicsquarelist,{7}];Length@p7
Out[]=
24060960
Convert the 7 numbers into a list of 16 lengths by filling in the light gray areas in the figure above.
In[]:=
subsets={#[[1]],#[[2]],0,#[[3]],#[[4]],#[[5]],#[[6]],0,0,0,#[[7]],0,0,0,0,0}&/@p7;
Prepare helper functions to solve the magic square.
In[]:=
(*Findthe9numbersthatarenotdisplayedinlightgrayinorder*)find9numbers[in_List]:=Fold[findnumber[#1,#2]&,in,{{{1,6,11},16},{{1,4,16},13},{{6,7,11},10},{{1,2,4},3},{{2,6,10},14},{{3,7,11},15},{{1,5,13},9},{{5,6,7},8},{{9,10,11},12}}];(*findanumberinspecificpositions*)findnumber[in_,{pos_List,tgt_}]:=Block[{n,remains},If[in==$Failed,Return[$Failed]];n=sagradafamiliamagicconstant-Total@in[[pos]];remains=remove[sagradafamiliamagicsquarelist,in];If[MemberQ[remains,n],ReplacePart[in,tgt->n],$Failed]];(*removespecificelementsfromalist*)remove[in_List,delete_List]:=Fold[DeleteCases[#,#2,{1},1]&,in,delete];
Try 24,060,960 combinations and find the solutions.
(The evaluation will take some time.)
(The evaluation will take some time.)
solves=Select[ParallelMap[find9numbers,subsets],ListQ];
In[]:=
solves=;
There are 1696 solutions. However, a solution to a magic square remains a solution even when reflection and/or a 90° rotation are applied. In other words, a solution to a magic square forms a group of 8 patterns. In this case, the number of solutions is 212.
In[]:=
Length@solves%/8
Out[]=
1696
Out[]=
212
Include the solution of the Magic Square on Sagrada Familia.
In[]:=
pos1=Position[solves,sagradafamiliamagicsquarelist][[1,1]]
Out[]=
5
In[]:=
Grid[Partition[solves[[pos1]],4],Frame->All]
Out[]=
1 | 14 | 14 | 4 |
11 | 7 | 6 | 9 |
8 | 10 | 10 | 5 |
13 | 2 | 3 | 15 |
The chosen one
The chosen one
I considered why the solution of Sagrada Familia was chosen from among 1,696 solutions.
The 16 numbers that make up the magic square, arranged in ascending order from the top left corner, are as follows.
Out[]=
1 | 2 | 3 | 4 |
5 | 6 | 7 | 8 |
9 | 10 | 10 | 11 |
13 | 14 | 14 | 15 |
I select the ones that matched this arrangement the most from among all the solutions. Specifically, I define closer solutions as those with a greater number matching it.
In[]:=
counts=ParallelMap[(Counts[#-sagradafamiliamagicsquareelements])[0]&,solves]/.{Missing["KeyAbsent",0]->0};pos2=PositionLargest[counts];Grid[Partition[solves[[#]],4],Frame->All,Background->{None,None,Thread[Position[Partition[solves[[#]]-sagradafamiliamagicsquareelements,4],0]->Cyan]}]&/@pos2
Out[]=
,
,
,
1 | 14 | 14 | 4 |
11 | 7 | 6 | 9 |
8 | 10 | 10 | 5 |
13 | 2 | 3 | 15 |
13 | 3 | 2 | 15 |
11 | 6 | 7 | 9 |
8 | 10 | 10 | 5 |
1 | 14 | 14 | 4 |
15 | 2 | 3 | 13 |
9 | 7 | 6 | 11 |
5 | 10 | 10 | 8 |
4 | 14 | 14 | 1 |
15 | 3 | 2 | 13 |
5 | 10 | 10 | 8 |
9 | 6 | 7 | 11 |
4 | 14 | 14 | 1 |
There are four solutions that match up to six locations(Cyan).
Of these four, only one has the number 1 in the upper left corner, so I suspect that this was chosen as he solution of Sagrada Familia
One of famous 4x4 magic squares is the magic square in Dürer’s engraving “Melencolia I”. The magic constant It is 34. The numbers “15” and “14” in the center of the bottom row represent the year of creation, with “1” and “4” placed at the ends of the bottom row for this purpose. Therefore, perhaps there are even deeper secrets hidden within the magic square of Sagrada Familia.
How many combinations?
How many combinations?
The magic constant 33, besides being the age at which Jesus was crucified, also may represent: The number of steps on the staircase in Park Guell, designed by Gaudi The highest degree in Freemasonry, and this is considered a supporting element for the theory that Gaudí was a Freemason A number significant in Japanese culture: Sanjusangendo temple
There are 310(=17+88+131+66+8) combinations in this magic square that add up to 33, including vertical, horizontal, and diagonal combinations.
In particular, the combinations of 4 numbers that add up to 33 reveal interesting patterns.
Prepare helper functions to display the pattern.
In[]:=
getpatterns[n_]:=Block[{subsets,combinations,res},subsets=Subsets[Range@16,{n}];combinations=Select[subsets,Total[sagradafamiliamagicsquarelist[[#]]]==sagradafamiliamagicconstant&];Print[ToString[Length@combinations]<>" combinations"];(*visualize*)res=Grid[Partition[sagradafamiliamagicsquarelist,4],Frame->All,Background->{None,None,Thread[(#/.list2mat)->Cyan]}]&/@combinations;GraphicsGrid[Partition[res,UpTo[8]],ImageSize->{600,Automatic}]];list2mat=ThreadRange@16->[{i,j},{i,4},{j,4}];
There are 17 combinations where the sum of three numbers equals 33.
In[]:=
getpatterns@3
17 combinations
There are 88 combinations where the sum of four numbers equals 33.
There are 131 combinations where the sum of five numbers equals 33.
There are 66 combinations where the sum of six numbers equals 33.
There are 8 combinations where the sum of seven numbers equals 33.
CITE THIS NOTEBOOK
CITE THIS NOTEBOOK
Solving the magic square on Sagrada Familia
by Kotaro Okazaki
Wolfram Community, STAFF PICKS, June 6, 2025
https://community.wolfram.com/groups/-/m/t/3475520
by Kotaro Okazaki
Wolfram Community, STAFF PICKS, June 6, 2025
https://community.wolfram.com/groups/-/m/t/3475520