Pick any four-digit number you like — just not one like 1111 or 2222, where every digit is the same. Now subtract the number formed by arranging that number's digits smallest-to-largest from the one formed largest-to-smallest. Then repeat the same calculation with whatever number comes out. Remarkably, no matter what number you started with, you're guaranteed to reach the number 6174 within at most 7 steps, and from then on, it's always 6174. This number is called Kaprekar's constant, named after the mathematician who discovered it.

Let's try starting from 3524. Largest-to-smallest gives 5432, smallest-to-largest gives 2345, and the difference is 5432−2345=3087. Repeat with 3087: largest-to-smallest gives 8730, smallest-to-largest gives 0378 (=378), and the difference is 8730−378=8352. Repeat again with 8352: largest-to-smallest gives 8532, smallest-to-largest gives 2358, and the difference is 8532−2358=6174! It reached 6174 in just three steps. Now do the same calculation with 6174 itself, and 7641−1467=6174 comes right back out — a fixed point that never changes again.

3524 3087 8352 6174 🎉
Even starting from 3524, it reaches 6174 in just 3 steps (every four-digit number takes at most 7)

Just like Kaprekar's constant, there are other numbers where a pattern reveals itself the more you repeat a calculation. Figurate numbers — numbers that form a shape when their count is arranged as dots — are a classic example. Stack dots row by row into a triangle, adding 1, then 2, then 3 dots per row, and you get the triangular numbers 1, 3, 6, 10, 15... (formula: n(n+1)/2). Stack them n-by-n into a square and you get the square numbers 1, 4, 9, 16, 25... (that is, n², the same as perfect squares). Stack dots layer by layer into a pentagon shape and you get the pentagonal numbers 1, 5, 12, 22, 35... (n(3n−1)/2). Turning a number into a visible shape like this makes it far more intuitive to understand exactly why each formula looks the way it does.

The third curious number is the Mersenne number — a number of the form 2ᵖ−1, formed by multiplying 2 by itself p times and subtracting 1. When p=2, you get 3; p=3 gives 7; p=5 gives 31 — sometimes these are prime, but not always. For example, even when p=11 (a prime), 2¹¹−1=2047=23×89 is not prime. In other words, p being prime doesn't guarantee that 2ᵖ−1 is prime too. Only when 2ᵖ−1 itself is prime is it specially called a Mersenne prime.

The real reason Mersenne primes are special lies in their relationship to perfect numbers. A perfect number is a number where adding up all its divisors except itself gives back the number itself (for example, 6=1+2+3, or 28=1+2+4+7+14). According to a theorem proven by the ancient Greek Euclid and, much later, Euler, whenever 2ᵖ−1 is a Mersenne prime, 2^(p−1)×(2ᵖ−1) is guaranteed to be a perfect number. For example, when p=2, 2¹×3=6; when p=3, 2²×7=28 — it lines up exactly. It's also been proven, conversely, that every even perfect number is built in exactly this form, and no other.

Mersenne primes are extremely rare. Humanity has found only 52 of them so far, so rare that discovering a new one, even with every computer resource thrown at the search, makes the news. On our activity page, you can pick a four-digit number yourself and check exactly how many steps the Kaprekar calculation takes to reach 6174, watch triangular, square, and pentagonal numbers stack up as dots with a slider, and change the value of p to calculate yourself which Mersenne numbers are prime and which perfect numbers they connect to.