Showing posts with label kaprekar. Show all posts
Showing posts with label kaprekar. Show all posts

Wednesday, 14 June 2023

Kaprekar Quintuples

I've written about Kaprekar before in posts titled Kaprekar's Routine on 16th July 2021 and Birth Year Magic on 3rd June 2020. Today, the number associated with my diurnal age (27100) brought me into contact with him again via his Kaprekar Quintuples. These are numbers \(n\) with the property that the digits of \(n^5\) taken \(d\) at a time sum to \(n\) (where \(d\) is the number of digits in \(n\)).

Let's illustrate this in the case of 27100 where we take groups of five digits beginning from the rightmost digit, leaving three digits remaining on the left:$$27100^5 = 14616603103510000000000\\ 146+16603+10351+00000+00000 = 27100 $$Let's try with another number, 7776, that has four digits instead of five. Here we take groups of four digits beginning at the rightmost digit with no digits left over:$$7776^5 = 28430288029929701376 \\2843+0288+0299+2970+1376 = 7776$$The members of the sequence are (permalink showing members up to 100,000):

1, 10, 1000, 7776, 27100, 73440, 95120, 500499, 505791, 540539, 598697, 665335, 697598, 732347, 7607610, 37944478, 46945205, 54995500, 55216205, 56607166, 58106906, 63136413, 66595563, 68167738, 68807564, 69188525, 70667477, 72197730, 73197730, 74145807

These numbers constitute OEIS A171500. These numbers can also be called order-5 Kaprekar numbers and this site shows the initial members. See Table 1.


Table 1

The same site also shows order-4 Kaprekar numbers or Kaprekar quadruples where numbers are raised to the fourth power. See Table 2.


Table 2

Order-3 and order-2 Kaprekar are also shown on this site and could be referred to as Kaprekar triplets/triples and Kaprekar couplets/pairs.

Friday, 16 July 2021

Kaprekar's Routine

I first encountered the algorithm that Wolfram MathWorld calls Kaprekar's Routine last year and actually made a post about it titled Birth Year Magic. I've created this post because I was reminded of the similarity between Kaprekar's Routine and the Odd-Even Algorithm that I've written about in a number of previous posts:



Dattaraya Ramchandra Kaprekar is something of an inspiration for me because our situations are similar academic-wise.

In Kaprekar's Routine, numbers get mapped to what are called fixed points (attractors in my terminology) or end up in loops (vortices in my terminology). Here is what Wolfram MathWorld has to say about the algorithm:

The Kaprekar routine is an algorithm discovered in 1949 by D. R. Kaprekar for 4-digit numbers, but which can be generalised to \(k\)-digit numbers. To apply the Kaprekar routine to a number \(n\), arrange the digits in descending \( n^{'} \) and ascending \( n^{''} \) order. Now compute \( K(n)=n^{'}-n^{''} \) (discarding any initial 0s) and iterate, where \(K(n)\) is sometimes called the Kaprekar function. The algorithm reaches 0 (a degenerate case), a constant, or a cycle, depending on the number of digits in \(k\) and the value of \(n\). The list of values is sometimes called a Kaprekar sequence, and the result K(n) is sometimes called a Kaprekar number (Deutsch and Goldman 2004), though this nomenclature should be deprecated because of confusing with the distinct sort of Kaprekar number.

In base-10, the numbers n for which \(K(n)=n\) are given by 495, 6174, 549945, 631764, ... (OEIS A099009). Similarly, the numbers \(n\) for which iterating \(K(n)\) gives a cycle of length \(k \geq 2\) are given by 53955, 59994, 61974, 62964, 63954, 71973, ... (OEIS A099010).

Iterating the Kaprekar map in base-10, all 1- and 2-digit numbers give 0. Exactly 60 3-digit numbers, namely 100, 101, 110, 111, 112, 121, 122, 211, 212, 221, ... (OEIS A090429), reach 0, while the rest give 495 in at most 6 iterations. Exactly 77 4-digit numbers, namely 1000, 1011, 1101, 1110, 1111, 1112, 1121, 1211, ... (OEIS A069746), reach 0, while the remainder give 6174 in at most 8 iterations. The value 6174 is sometimes known as Kaprekar's constant (Deutsch and Goldman 2004). This pattern breaks down for 5-digit numbers, which may converge to 0 or one of the 10 constants 53955, 59994, 61974, 62964, 63954, 71973, 74943, 75933, 82962, 83952.

I developed an SageMath algorithm (permalink) to determine the behaviour of numbers of any length over a given range. In my earlier post, my algorithms only applied to three and four digit numbers. Here is the behaviour for numbers in the range from 26400 to 26425:

26400 --> [26400, 63954, 61974, 82962, 75933, 63954]

26401 --> [26401, 62964, 71973, 83952, 74943, 62964]

26402 --> [26402, 61974, 82962, 75933, 63954, 61974]

26403 --> [26403, 61974, 82962, 75933, 63954, 61974]

26404 --> [26404, 61974, 82962, 75933, 63954, 61974]

26405 --> [26405, 62964, 71973, 83952, 74943, 62964]

26406 --> [26406, 63954, 61974, 82962, 75933, 63954]

26407 --> [26407, 73953, 63954, 61974, 82962, 75933, 63954]

26408 --> [26408, 83952, 74943, 62964, 71973, 83952]

26409 --> [26409, 93951, 85932, 74943, 62964, 71973, 83952, 74943]

26410 --> [26410, 62964, 71973, 83952, 74943, 62964]

26411 --> [26411, 52965, 70983, 94941, 84942, 73953, 63954, 61974, 82962, 75933, 63954]

26412 --> [26412, 51975, 81972, 85932, 74943, 62964, 71973, 83952, 74943]

26413 --> [26413, 51975, 81972, 85932, 74943, 62964, 71973, 83952, 74943]

26414 --> [26414, 51975, 81972, 85932, 74943, 62964, 71973, 83952, 74943]

26415 --> [26415, 52965, 70983, 94941, 84942, 73953, 63954, 61974, 82962, 75933, 63954]

26416 --> [26416, 53955, 59994, 53955]

26417 --> [26417, 63954, 61974, 82962, 75933, 63954]

26418 --> [26418, 73953, 63954, 61974, 82962, 75933, 63954]

26419 --> [26419, 83952, 74943, 62964, 71973, 83952]

26420 --> [26420, 61974, 82962, 75933, 63954, 61974]

26421 --> [26421, 51975, 81972, 85932, 74943, 62964, 71973, 83952, 74943]

26422 --> [26422, 41976, 82962, 75933, 63954, 61974, 82962]

26423 --> [26423, 41976, 82962, 75933, 63954, 61974, 82962]

26424 --> [26424, 41976, 82962, 75933, 63954, 61974, 82962]

26425 --> [26425, 42966, 71973, 83952, 74943, 62964, 71973]

All the numbers end in a loop of one type or another. Let's compare what happens to today's number (I'm 26402 days old) under the two different algorithms:

  • Kaprekar's Routine:

    26402
    --> [26402, 61974, 82962, 75933, 63954, 61974]

  • Odd Even Algorithm: 

    26402
    -->[26402, 26388, 26367, 26363, 26355, 26360, 26349]
Kaprekar's Routine causes the number to end up in a loop [61974, 82962, 75933, 63954, 61974] while the Odd Even Algorithm leads to an attractor, or fixed point, 26349. Go back to my post Birth Year Magic to find out more about Kaprekar.

Wednesday, 3 June 2020

Birth Year Magic

Everybody currently living was born in a year that contains four digits. I was born in 1949. My granddaughter was born in 2002. Let's take her year of birth and arrange the digits into ascending and descending order to form two new numbers: 2200 and 0022 (or simply 22). Let's subtract the smaller from the larger. The result is 2178. Let's repeat that process to from 8721 and 1278. Subtraction yields 7443. Continuing this process we get 3996, 6264, 4176 and finally 6174. Why finally? Because subtracting 7641 and 1467 leads us back to 6174! So for 2002, the progression is 2178, 7443, 3996, 6264, 4176, 6174.

Remarkably, all birth years lead to this very same number. Let's take my year of birth: the progression is 1949, 8442, 5994, 5355, 1998, 8082, 8532, 6174. The number 6174 is known as Kaprekar's constant and because it involves four digit numbers with at least two digits different, it's ideally suited to current birth years. You're not going to find someone who was born in 1111 or 2222 who will spoil the trick and so it's an ideal party trick. Everyone has their smartphone near at hand and, if not, one can always be borrowed. Using a calculator app or simply the Chrome browser, the necessary calculations can be quickly and easily carried out.

Figure 1: Google Chrome's Calculator

So Kaprekar's constant connects everybody alive today and in fact everybody born, or still to be born, between the years 1111 and 2222 with those two limits excluded.


There is no other four digit number that returns itself under the operation of subtract digits in ascending order from digits in descending order. There is a nice proof of why this is so on this site that also alerted me to the fact that for three digit numbers there is a constant that is reached as well, namely 495.

Suppose we have four digits \(a,b,c,d\) such that \(9 \geq a>b>c>d \geq 0\). If we can get the same digits (in any order) arising after the subtraction of \(dcba\) from \(abcd\), then we have an unchanging constant. It turns out that that only \(abcd-dcba=bdac\) satisfies and when this is the case \(a=7, b=6, c=4, d=1\). It's the same for the three digit situation \(abc\) and \(cba\) where \(9 \geq a>b>c \geq 0\). Here only \(abc-cba=cab\) satisfies and \(a=9,b=5,c=4\).

A table is also provided showing the situation for numbers of lengths other than 3 and 4. It can be seen that only three and four digit numbers lead to a unique constant. See Figure 1.

Figure 1: source

The site also shows the frequencies of the number of iterations required for four digit numbers to reach 6174. See Figure 2.

Figure 2: source

Here a permalink to some SageMath code that will display, for four digit numbers, the progression in reaching 6174 and count the number of iterations required. Here is another permalink, this time for three digit numbers.

Kaprekar is an interesting mathematician (biography link) and he is also associated with:
  • Kaprekar numbers (Wikipedia link)
  • Harshad numbers (see my blog posts: here and here)
  • Self numbers and Junction numbers (see my blog post here)
Finally we should say a little about the number 6174 and some of its properties. One interesting property is that it can be written as the sum of the first three degrees of 18:$$18^3 + 18^2 + 18^1 = 5832 + 324 + 18 = 6174$$This relationship also means that 6174 is a Harshad number because it is divisible by the sum of its digits (18).