Skip to content

Python package to perform Kaprekar's routine for 4 digit numbers

License

Notifications You must be signed in to change notification settings

shashankdeshpande/kaprekar_constant

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Kaprekar's constant (6174)

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

  • Take any four-digit number (at least two digits should be different)
  • Arrange the digits in descending and ascending order to get two new four-digit numbers
  • Now, subtract the smaller number from the bigger number
  • Go back to step 2 and repeat

This process is called Kaprekar's routine. The routine states that you'll always reach number 6174 in at most 7 iterations and once you reach 6174, the process will continue generating the same number. How cool is that?

Installation

Install directly from the GitHub repository

pip install git+https://github.com/shashankdeshpande/kaprekar_constant.git 

Usage

Import package:

>>> import kaprekar_constant as kc

Perform Kaprekar Routine:

>>> obj = kc.kaprekar_routine(1913)
>>> obj
<kaprekar_constant.object.Kaprekar at 0x7f1ae2f085c0>

Check results:

>>> obj.steps
['9311 - 1139 = 8172',
 '8721 - 1278 = 7443',
 '7443 - 3447 = 3996',
 '9963 - 3699 = 6264',
 '6642 - 2466 = 4176',
 '7641 - 1467 = 6174']
>>> obj.step_count
6
>>> obj.constant
6174

References