๐Ÿ”ข Magic Square Puzzle

Arrange numbers 1 to 9 so every row, column, and diagonal sums to 15

โ†˜ Diag 1: 0
โ†™ Diag 2: 0
0 / 15
0 / 15
0 / 15
0 / 15
0 / 15
0 / 15
Available Numbers Click a cell or number to place
๐Ÿ“š How It Works & The Magic Math โ–ผ

1. Why is the Magic Constant 15?

The sum of integers from 1 to 9 is: 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45.

Since the square has 3 identical rows that must share this sum equally: 45 รท 3 = 15.

2. Why Must 5 Always Be in the Center?

The center cell belongs to 4 lines (row 2, column 2, and both diagonals). Notice that the available numbers form 4 pairs that add up to 10 with 5: (1, 9), (2, 8), (3, 7), and (4, 6).

Only the number 5 can balance all 4 lines simultaneously!

3. The Ancient "Lo Shu" Square

The 3x3 magic square was discovered in ancient China over 2,500 years ago, legendarily inscribed on the shell of a turtle emerging from the River Luo. There are exactly 8 variations of this square, all formed by rotating and reflecting this single fundamental pattern.

4. TypeScript Learning Highlights in this Project

  • Type Safety: Strict types for cells (number | null) prevent invalid data.
  • Pure Functions: src/logic.ts isolates all math logic with no DOM dependencies, making it 100% testable.
  • DOM Casting: src/main.ts safely uses TypeScript type assertions (e.g. HTMLButtonElement).
  • State Management: Single source of truth for board state, selection, and history.
TypeScript Code Example