HOEKSTRA.CO.UK

Let's play with the magical number that is known as Kaprekar's Constant, the discoveror of this number: 6174

When we take any 4 random digits, of which at least 1 must be different from the other 3, arrange them from smallest to largest, and then subtract it from the same set of digits that have been arranged from largest to smallest, the result may well be 6174. If the result if not this number, then use the result and arrange them into 2 numbers in the same way and subtract them from each other. Repeat this until the result is 6174. Were you to repeat this process on this value of 6174, the result will be 6174 again, and so on. Sometimes, it takes only 1 such iteration, sometimes more. Either way, it seems impossible to determine in advance how many such iterations are required to reach convergence to 6174. Or is there?

Example

Choose 4 random numbers: 7346

Iteration 1: 7346
Arranged largest to smallest: 7643
Arranged smallest to largest: 3467
Subtract the one from the other: 4176
Iteration 2: 4176
Arranged largest to smallest: 7641
Arranged smallest to largest: 1467
Subtract the one from the other: 6174 

We have convergence! In this case, we only need 2 such iterations for this convergence.

It shouldn't come as a surprise that Kaprekar's Constant itself immediately converges to itself. Let's test this:

Iteration 1: 6174
Arranged largest to smallest: 7641
Arranged smallest to largest: 1467
Subtract the one from the other: 6174

Yup, that is Kaprekar's Constant alright.

How many iterations to convergence?

What 4-digit value, bearing in mind that at least 1 digit must be different from the others, will cause the most iterations before converging to 6174? Can we determine in advance how many such iterations would be required to a given value?

Let's take a look, by trying this for all values up to 9999. Note than many values will have the same outcome, since they would contain the same digit combinations, e.g. 5432 would have the same result as 2345, 3452, 4523, 5423, etc.

Since there is a fair bit of processing to do for each value, it would make sense to put this in a function, rather than in-line it in a BASH one-liner. This function determines the number of iterations for a given input value and then outputs that input value and the found result to stdout. It is up to the code that called this function, what it does with this output.

function kapit {
  itcount=0
  └─ iteration counter ─┘
  result=$(printf "%04d" "$1")
 └─ our working intermediate result, starting with the 0 left-padded input parameter ─┘
  while :
  └─ this means loop forever ─┘
  do
    s1=$(echo "$result" | grep -o . | sort | tr -d "\n" | sed -e 's/^0\{1,3\}//' ) 
                       └─ put each digit on a new line ─┘                                                                           └─ arrange the lines in numerical order ─┘
                                                                                            └─ join the lines up into a singel line ─┘
                                                                                                                         └─ remove up to 3 leading 0s ─┘
    s2=$(echo "$result" | grep -o . | sort -r | tr -d "\n" | sed -e 's/^0\{1,3\}//' ) 
                       └─ put each digit on a new line ─┘                                                        
                                                                            └─ arrange the lines in reverse numerical order ─┘
                                                                                                 └─ join the lines up into a singel line ─┘
                                                                                                                             └─ remove up to 3 leading 0s ─┘
    result=$(bc <<< "$s2"-"$s1")
                      └─ subtract the larger from the smaller ─┘
    [[ $result == 0 ]] && break 
    └─ this was a 0,1111, 2222,..., so stop ─┘
    ((itcount+=1))
    └─ increment the iteration count ─┘
    [[ $result == 6174 ]] && break
    └─ we have converged, so stop ─┘
    result=$(printf "%04d" "$result")
    └─ left-pad with 0s to 4 digits for the next iteration ─┘
  done
  echo "$1" "$itcount"
  └─ output to stdout ─┘
}
 
We call this function for each value from 0 to 9999 and redirect the output from each function-call to a file called kapit-results.txt, which should give us a two-column data set:
 
$ for i in {0..9999}; do kapit "$i"; done > kapit-results.txt
 
A quick look at the output tells us that we are on the right track:
 

$ head  kapit-results.txt  
0 0
1 5
2 4
3 6
4 4
5 6
6 6
...

Plotting the data

A simple plot of the two-column data file, using GnuPlot, might give us some insight into the posed questions:

$ gnuplot
...
gnuplot> plot "kapit-results.txt"
... and when you tired of viewing the plot, you can exit gnuplot with:
gnuplot> exit 

 

What can we infer from the graph?

1. The number of iterations that any given value requires to convergence is pretty much random. 
2. Most values take 3 iterations to convergence
3. There are never more than 7 iterations to convergence.
 
Who knows, there could be a profound pattern hidden in there in the apparently random data that ranges from 1 to 7, but for now it seems impossible to determine in advance how many iterations to convergence a given number would require. This has been one of life's futile and pointless exercises.