ArticleslgStudy

computer science

Kaprekar's routine

Kaprekar's routine is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Kaprekar's routine rather than just read about it. In short: In number theory, Kaprekar's routine is an iterative algorithm named after its inventor, Indian mathematician D. R.

Key takeaways

  • Kaprekar's routine belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Kaprekar's routine to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Kaprekar's routine from memory before moving on to harder problems.

Reference excerpt

In number theory, Kaprekar's routine is an iterative algorithm named after its inventor, Indian mathematician D. R. Kaprekar. Each iteration starts with a four-digit random number, sorts the digits into descending and ascending order, and calculates the difference between the two new numbers. As an example, starting with the number 8991 in base 10:

9981 – 1899 = 8082 8820 – 288 = 8532 8532 – 2358 = 6174 7641 – 1467 = 6174 6174, known as Kaprekar's constant, is a fixed point of this algorithm. Any four-digit number (in base 10) with at least two distinct digits will reach 6174 within seven iterations. The algorithm runs on any natural number in any given number base.

Definition and properties The algorithm is as follows:

Choose any four-digit natural number n {\displaystyle n} in a given number base b {\displaystyle b} . This is the first number of the sequence. Create a new number α {\displaystyle \alpha } by sorting the digits of n {\displaystyle n} in descending order, and another number β {\displaystyle \beta } by sorting the digits of n {\displaystyle n} in ascending order. These numbers may have leading zeros, which can be ignored. Subtract α − β {\displaystyle \alpha -\beta } to produce the next number of the sequence. Repeat step 2. The sequence is called a Kaprekar sequence and the function K b ( n ) = α − β {\displaystyle K_{b}(n)=\alpha -\beta } is the Kaprekar mapping. Some numbers map to themselves; these are the fixed points of the Kaprekar mapping, and are called Kaprekar's constants. Zero is a Kaprekar's constant for all bases b {\displaystyle b} , and so is called a trivial Kaprekar's constant. All other Kaprekar's constants are nontrivial Kaprekar's constants. For example, in base 10, starting with 3524,

K 10 ( 3524 ) = 5432 − 2345 = 3087 {\displaystyle K_{10}(3524)=5432-2345=3087}

K 10 ( 3087 ) = 8730 − 378 = 8352 {\displaystyle K_{10}(3087)=8730-378=8352}

K 10 ( 8352 ) = 8532 − 2358 = 6174 {\displaystyle K_{10}(8352)=8532-2358=6174}

K 10 ( 6174 ) = 7641 − 1467 = 6174 {\displaystyle K_{10}(6174)=7641-1467=6174}

with 6174 as a Kaprekar's constant. All Kaprekar sequences will either reach one of these fixed points or will result in a repeating cycle. Either way, the end result is reached in a fairly small number of steps (within seven iterations or steps). Note that the numbers α {\displaystyle \alpha } and β {\displaystyle \beta } have the same digit sum and hence the same remainder modulo b − 1 {\displaystyle b-1} . Therefore, each number in a Kaprekar sequence of base b {\displaystyle b} numbers (other than possibly the first) is a multiple of b − 1 {\displaystyle b-1} . When leading zeroes are retained, only repdigits lead to the trivial Kaprekar's constant. In base 4, it can easily be shown that all numbers of the form 3021, 310221, 31102221, 3...111...02...222...1 (where the length of the "1" sequence and the length of the "2" sequence are the same) are fixed points of the Kaprekar mapping. In base 10, it can easily be shown that all numbers of the form 6174, 631764, 63317664, 6...333...17...666...4 (where the length of the "3" sequence and the length of the "6" sequence are the same) are fixed points of the Kaprekar mapping.

Determination of Kaprekar numbers In the following, "Kaprekar's constant k" refers to a number that becomes a positive fixed point k as a result of Kaprekar's routine. In 1981, G. D. Prichett, et al. showed that the Kaprekar's constants in base 10 are limited to two numbers, 495 (3 digits) and 6174 (4 digits). They also classified the Kaprekar numbers into four types, but there was some overlap in the classification. In 2005, Y. Hirata calculated all fixed points up to 31 decimal digits and examined their distribution. In 2024, Haruo Iwasaki of the Ranzan Mathematics Study Group (headed by Kenichi Iyanaga) showed that in order for a natural number to be a Kaprekar number, it must belong to one of five mutually disjoint sets composed of combinations of the seven numbers 495, 6174, 36, 123456789, 27, 875421 and 09. Iwasaki also showed that this new classification using the five sets includes a corrected classification by Prichett, et al. As a result, if n is considered as a constant, then the number of decimal n-digit Kaprekar numbers is determined by two types of equations:

(1) n = 3 x ( x ≥ 1 ) , {\displaystyle n=3x\quad \quad (x\geq 1)\,,} ......... For the sequence of x 3-digit constants 495 (2) n = 4 + 2 x ( x ≥ 0 ) , {\displaystyle n=4+2x\quad (x\geq 0)\,,} ...... Sequence of 4-digit constant 6174 followed by x 2-digit constants 36 or by three types of Diophantine equations:

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Kaprekar's routine

Start with the simplest possible case. Write down what Kaprekar's routine claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Kaprekar's routine before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Kaprekar's routine ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Kaprekar's routine

In research
Kaprekar's routine appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Kaprekar's routine in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Kaprekar's routine is common in secondary-school and first-year university syllabi. It links to neighbouring topics Arithmetic dynamics, Base-dependent integer sequences, Sorting algorithms, so understanding it makes those chapters shorter.
In everyday life
Look for Kaprekar's routine outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Kaprekar's routine” →

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Kaprekar's routine in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Kaprekar's routine means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Kaprekar's routine out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Kaprekar's routine in simple terms?

In number theory, Kaprekar's routine is an iterative algorithm named after its inventor, Indian mathematician D. R.

Why does Kaprekar's routine matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Kaprekar's routine?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Kaprekar's routine.

Tags

  • Arithmetic dynamics
  • Base-dependent integer sequences
  • Sorting algorithms

Keep exploring