# How many times to Kaprekar’s constant using Python ## Introduction

6174 is known as Kaprekar’s constant after the Indian mathematician D. R. Kaprekar. This number is notable for the following rule:

1. Take any four-digit number, using at least two different digits (leading zeros are allowed).
2. Arrange the digits in descending and then in ascending order to get two four-digit numbers, adding leading zeros if necessary.
3. Subtract the smaller number from the bigger number.
4. Go back to step 2 and repeat.

The above process, known as Kaprekar’s routine, will always reach its fixed point, 6174, in at most 7 iterations. Once 6174 is reached, the process will continue yielding 7641 – 1467 = 6174. For example, choose 3524:5432 – 2345 = 30878730 – 0378 = 83528532 – 2358 = 61747641 – 1467 = 6174

The only four-digit numbers for which Kaprekar’s routine does not reach 6174 are repdigits such as 1111, which give the result 0000 after a single iteration. All other four-digit numbers eventually reach 6174 if leading zeros are used to keep the number of digits at 4.

## Finding the number of times using Python

To learn how to sort an integer in python, check out this tutorial on how to sort an integer.

```.wp-block-code{border:0;padding:0}.wp-block-code>div{overflow:auto}.shcb-language{border:0;clip:rect(1px,1px,1px,1px);-webkit-clip-path:inset(50%);clip-path:inset(50%);height:1px;margin:-1px;overflow:hidden;padding:0;position:absolute;width:1px;word-wrap:normal;word-break:normal}.hljs{box-sizing:border-box}.hljs.shcb-code-table{display:table;width:100%}.hljs.shcb-code-table>.shcb-loc{color:inherit;display:table-row;width:100%}.hljs.shcb-code-table .shcb-loc>span{display:table-cell}.wp-block-code code.hljs:not(.shcb-wrap-lines){white-space:pre}.wp-block-code code.hljs.shcb-wrap-lines{white-space:pre-wrap}.hljs.shcb-line-numbers{border-spacing:0;counter-reset:line}.hljs.shcb-line-numbers>.shcb-loc{counter-increment:line}.hljs.shcb-line-numbers .shcb-loc>span{padding-left:.75em}.hljs.shcb-line-numbers .shcb-loc::before{border-right:1px solid #ddd;content:counter(line);display:table-cell;padding:0 .75em;text-align:right;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;white-space:nowrap;width:1%}```# Taken from http://ao.gl/how-to-sort-an-integer-in-python
def sort_asc(n):
return int("".join(sorted([i for i in str(n)])))

# Taken from http://ao.gl/how-to-sort-an-integer-in-python
def sort_desc(n):
return int("".join(sorted([i for i in str(n)], reverse=True)))

def kaprekar_times(n):
# count how many times
count = 0

# set the last answer to compare against
last = n

# loop forever
while True:
# get the smallest and biggest
smaller = sort_asc(last)
bigger = sort_desc(last)

# increment
count += 1

# return the count if it's a match
return count
else:
# otherwise continue

# return if all else fails
return count

# Some test cases
print(kaprekar_times(9272)) # 6
print(kaprekar_times(1263)) # 8
print(kaprekar_times(9820)) # 8
print(kaprekar_times(2489)) # 4
```Code language: Python (python)```
Tags:
Subscribe
Notify of 