Sitemap
Level Up Coding

Coding tutorials and news. The developer homepage gitconnected.com && skilled.dev && levelup.dev

Member-only story

This Math Problem Is 3,000 Years Old — Magic Squares

13 min readSep 11, 2026

--

Press enter or click to view image in full size
Image based on Albrecht Dürer engraving, 1514

If you’re not a Medium member, a video version is available on YouTube.

There is a math problem that is literally ancient, and it belongs to the realm of recreational mathematics. And while doing this, we can learn a lot of interesting things. In this article, I will write about the so-called magic squares, and we will try to find them using a modern computer and a CUDA GPU. As we will see, the problem is more challenging than we may think. It will also be a fun exercise if you want to learn a bit of C++, OpenMP, multithreading, and CUDA.

Let’s get started! As usual, if you want to see a coding process in action, a video version is added at the end of the article.

1. History

First, what is a magic square? Obviously, it is a square (surprise:) of size N, filled with unique numbers from 1 to N², where the sums of the horizontal, vertical, and diagonal lines are the same.

Press enter or click to view image in full size
Image source — Wikipedia

3x3 magic squares were independently discovered in ancient China, India, Japan, and other places…

--

--

Dmitrii Eliuseev
Dmitrii Eliuseev

Written by Dmitrii Eliuseev

Hi everyone! I am a Python/IoT developer, science and electronics enthusiast, and my goal here is to share my knowledge with you. Stay tuned and subscribe ;)