Fun with Numbers - Kaprekar’s Constant

Akintunde Ayodele
Published in
4 min readJan 24


Photo by Black ice:

Let’s have some fun with numbers.

Pick a four-digit number with at least two distinct digits(monodigit numbers like 1111, 8888, are not going to work).

Apply the following operations on the chosen number:

  1. Rearrange the digits in descending order - this will give the biggest number that can be formed by those digits.
  2. Rearrange the digits of the original number in ascending order or just reverse the number obtained in step 1 - this gives the smallest number that can be formed by those digits.
  3. Subtract the number obtained in step 2 from the one obtained in step 1. What did you get? If it is not 6174, repeat steps 1 to 3 on your result.

If you continue the routine, you are guaranteed to land on 6174 eventually and you should stop there, otherwise, you get into a loop of repeating 6174.

This happens with any four-digit number you start with.

An Example

Let’s work with an actual number, say 5628:

  1. Rearrange 5628 to get 8652.
  2. Reverse it to get 2568.
  3. 8652 - 2568 = 6084.

Repeat the steps with 6084:

8640 - 0468 = 8172
8721 - 1278 = 7443
7443 - 3447 = 3996
9963 - 3699 = 6264
6642 - 2466 = 4176
7641 - 1467 = 6174.

The number 6174 is known as Kaprekar’s constant, named after D. R. Kaprekar, the Indian mathematician who discovered it.

It is easy to see why we get stuck at 6174, the reason is, 6174(or any of it’s rearrangements) has the property that when you apply the routine on it, you get the same set of digits 1, 4, 6, 7 contained in the original number in the result, even on the first run. But, why do we always end up with 6174 and not some other permutation of the digits like 6147, 1746, 4761? And can we prove that we will always get to 6174? We will attempt the later question in the next section.

Proving It

How do we prove that we will always land at 6174 for all four-digit numbers? This shouldn’t be too hard to prove, unlike the infamous, proof-defying Collatz Conjecture. Or is it?

Well, there are at least two ways we could prove this. The first method is a crude form of proof by cases - since there are a finite number of four-digit numbers in the world, we could just verify the observation for each and every one of them. It will take finite time.

What looks like a more elegant method is to use algebra - represent the general four-digit number by letters say, ABCD. By place value, we know this is A thousand, B hundred, C tens and D units. Expanding it gives ABCD = 1000A + 100B + 10C + D. Without loss of generality, we can assume the digits are already sorted, that is, A > B > C > D.

The reverse of ABCD is DCBA. Using place value and subtracting, we have ABCD - DCBA = (1000A + 100B + 10C + D) - (1000D + 100C + 10B + A)
= 999A + 90B - 90C - 999D.
= 999(A - D) + 90(B - C).

Now, that’s a hint. See if you can complete the algebraic proof on your own. Let me know how it goes. Good luck.

The example with 5628 took 7 steps to get to 6174. It is known that 7 is the maximum number of steps it takes to get to the Kaprekar’s constant for four-digit numbers. It will be nice to have a proof of this too.

A Fun Script

I wrote a little script to show the steps of the Kapreka’s routine for any input number, you can see it in action here.

Fun Activities

  1. Verify by hand the Kapreka phenomenon for at least ten different four-digit numbers.
  2. Write a computer program in your favorite language to calculate Kaprekar’s constant for all four-digit numbers. Also return the number of steps it takes for each number. Hint: All four-digit numbers are in the range 1000 to 9999.
  3. Try to discover other Kaprekar’s constants for three-digit, five-digit, six-digit, seven-digit numbers, etc.
  4. There are some redundant numbers in the range 1000 - 9999, for example 1234, 4321, 1243, 2413, and other permutations of the digits 1, 2, 3, 4, are all equivalent, you need to carry out the Kaprekar routine on just one of them, not all. Try to figure out a systematic way to detect all the redundancies and smartly incorporate the optimization in your computer program in Activity 2.

Math can be fun!

[ Written by a human ]



Akintunde Ayodele

Programmer. Mathematician. Thinker. Entrepreneur, among other things. Give me a place to sit and I will move the world.