Coding Kaprekar’s Constant
My father, Dr. Shriram Chauthaiwale, is a passionate mathematician specialized in Vedic Mathematics and History of Indian Mathematics. His research involves interpreting cryptic ancient manuscripts and translating those into contemporary mathematical language/notations. Sometimes, he throws a challenge at me for converting a tedious mathematical equations/formula into a computer program. This not only helps him validate his manual computations but also speeds up the validation process. This article is about an interesting mathematical concept called Kaprekar’s constant (number 6174) and how we solved a related problem with a computer program.
Kaprekar’s Constant
Number 6174 is known as Kaprekar’s constant after the Indian mathematician D. R. Kaprekar. This constant is special because any four digit number ( in which all four digits are NOT identical) can be converted into Kaprekar’s constant in finite steps. Following are the steps to obtain Kaprekar’s constant.
- Take a four digit number: E.g. 2435
- Rearrange the digits it to form the largest and smallest numbers: 5432 and 2345.
- Subtract lower number from higher: 5432–2345 = 3087
- Repeat Steps 2–3 till we get 6174
- From 3087: 8730–0378 = 8352
- From 8352: 8532–2358 = 6174
D R Kaprekar observed that any 4 digit number can be converted to 6174 in maximum 7 iterations of above algorithm.
This concept is well known in mathematics world. In fact, a few websites like HackerRank used this as a programming challenge. However, we wanted to find out an answer to a very specific question. Let’s dive into it !
Problem Statement
After understanding Kaprekar’s constant, Let’s try to understand the problem which we attempted. As mentioned above, any number between (0–9999) can be converted into Kaprekar constant in maximum 7 iterations. We wanted to dig deeper a bit and categorize numbers into 7 buckets, each representing number of iterations required. This is tedious process if has to be done manually for all 9999 numbers. Therefore, we decided to take help of the computer. Following is the implementation of the problem in Python
Code
Input: numbers between 1–9999
(Note: Even though this method works for 4 digit number, we can convert lesser digit numbers by adding leading zeroes. E.g. 1 => 0001)
Output:
Bucket 1: Numbers which requires 1 Iteration => [ …….]
Bucket 2: Numbers which requires 2 Iterations => [ …….]
Bucket 3: Numbers which requires 3 Iterations => [ …….]
Bucket 4: Numbers which requires 4 Iterations => [ …….]
Bucket 5: Numbers which requires 5 Iterations => [ …….]
Bucket 6: Numbers which requires 6 Iterations => [ …….]
Bucket 7: Numbers which requires 7 Iterations => [ …….]
Results
Here is the distribution of numbers for each bucket from 1–7.

Conclusion
The algorithm/code looks fairly simple to implement. However, it definitely helped a mathematician complete his job quickly. Isn’t the whole point of being a programmer is solving problems to make someone’s life easier?
