Kaprekar's routine
In number theory, Kaprekar's routine is an iterative algorithm named after its inventor, Indian mathematician D. R. Kaprekar. Each iteration starts with a 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 – 0288 = 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.[1] The algorithm runs on any natural number in any given number base.
Definition and properties
[edit]The algorithm is as follows:[2]
- Choose any natural number in a given number base . This is the first number of the sequence.
- Create a new number by sorting the digits of in descending order, and another number by sorting the digits of in ascending order. These numbers may have leading zeros, which can be ignored. Subtract to produce the next number of the sequence.
- Repeat step 2.
The sequence is called a Kaprekar sequence and the function is the Kaprekar mapping. Some numbers map to themselves; these are the fixed points of the Kaprekar mapping,[3] and are called Kaprekar's constants. Zero is a Kaprekar's constant for all bases , 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,
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.
Note that the numbers and have the same digit sum and hence the same remainder modulo . Therefore, each number in a Kaprekar sequence of base numbers (other than possibly the first) is a multiple of .
When leading zeroes are retained, only repdigits lead to the trivial Kaprekar's constant.
Families of Kaprekar's constants
[edit]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.
b = 2k
[edit]It can be shown that all natural numbers
are fixed points of the Kaprekar mapping in even base b = 2k for all natural numbers n.
k | b | m |
---|---|---|
1 | 2 | 011, 101101, 110111001, 111011110001... |
2 | 4 | 132, 213312, 221333112, 222133331112... |
3 | 6 | 253, 325523, 332555223, 333255552223... |
4 | 8 | 374, 437734, 443777334, 444377773334... |
5 | 10 | 495, 549945, 554999445, 555499994445... |
6 | 12 | 5B6, 65BB56, 665BBB556, 6665BBBB5556... |
7 | 14 | 6D7, 76DD67, 776DDD667, 7776DDDD6667... |
8 | 16 | 7F8, 87FF78, 887FFF778, 8887FFeFF7778... |
9 | 18 | 8H9, 98HH89, 998HHH889, 9998HHHH8889... |
Kaprekar's constants and cycles of the Kaprekar mapping for specific base b
[edit]All numbers are expressed in base b, using A−Z to represent digit values 10 to 35.
Base b | Digit length | Nontrivial (nonzero) Kaprekar's constants | Cycles |
---|---|---|---|
2 | 2 | 01[note 1] | ∅ |
3 | 011[note 1] | ∅ | |
4 | 0111,[note 1] 1001 | ∅ | |
5 | 01111,[note 1] 10101 | ∅ | |
6 | 011111,[note 1] 101101, 110001 | ∅ | |
7 | 0111111,[note 1] 1011101, 1101001 | ∅ | |
8 | 01111111,[note 1] 10111101, 11011001, 11100001 | ∅ | |
9 | 011111111,[note 1] 101111101, 110111001, 111010001 | ∅ | |
10 | 0111111111,[note 1] 1011111101, 1101111001, 1110110001, 1111000001 | ∅ | |
3 | 2 | ∅ | ∅ |
3 | ∅ | 022 → 121 → 022[note 1] | |
4 | ∅ | 1012 → 1221 → 1012 | |
5 | 20211 | ∅ | |
6 | ∅ | 102212 → 210111 → 122221 → 102212 | |
7 | 2202101 | 2022211 → 2102111 → 2022211 | |
8 | 21022111 | ∅ | |
9 | 222021001 |
220222101 → 221021101 → 220222101 202222211 → 210222111 → 211021111 → 202222211 | |
10 | 2210221101 |
1022222212 → 2102222111 → 2111011111 → 1222222221 → 1022222212 | |
4 | 2 | ∅ | 03 → 21 → 03[note 1] |
3 | 132 | ∅ | |
4 | 3021 | 1332 → 2022 → 1332 | |
5 | ∅ | 20322 → 23331 → 20322 | |
6 | 213312, 310221, 330201 | ∅ | |
7 | 3203211 | ∅ | |
8 | 31102221, 33102201, 33302001 | 22033212 → 31333311 → 22133112 → 22033212 | |
9 | 221333112, 321032211, 332032101 | ∅ | |
10 | 3111022221, 3220332111, 3311022201, 3331022001, 3333020001 | ∅ | |
5 | 2 | 13 | ∅ |
3 | ∅ | 143 → 242 → 143 | |
4 | 3032 | ∅ | |
5 | ∅ | 30432 → 40431 → 42411 → 32412 → 30432 | |
6 | ∅ | 214423 → 320322 → 304432 → 414421 → 331212 → 214423 | |
7 | ∅ | 3204322 → 4104331 → 4314211 → 3314212 → 3204322 | |
8 | ∅ | 30444432 → 42103321 → 42144221 → 33144212 → 33103312 → 32203222 → 30444432 | |
9 | 432043211 | ∅ | |
10 | ∅ | 3044444432 → 4210443321 → 4331033111 → 4222122221 → 3044444432 | |
6 | 2 | ∅ | 05 → 41 → 23 → 05[note 1] |
3 | 253 | ∅ | |
4 | ∅ | 1554 → 4042 → 4132 → 3043 → 3552 → 3133 → 1554 | |
5 | 41532 | 31533 → 35552 → 31533 | |
6 | 325523, 420432, 530421 | 205544 → 525521 → 432222 → 205544 | |
7 | ∅ | 4405412 → 5315321 → 4405412 | |
8 | 43155322, 55304201 |
31104443 → 43255222 → 33204323 → 41055442 → 54155311 → 44404112 → 43313222 → 31104443 42104432 → 43204322 → 42104432 53104421 → 53304221 → 53104421 | |
9 | 332555223 | 441054412 → 533153221 → 441054412 | |
10 | 4321044322, 4421553312, 5331044221, 5553042001 |
4211044432 → 4332043222 → 4211044432 5311044421 → 5333042221 → 5311044421 5531044201 → 5533042201 → 5531044201 | |
7 | 2 | ∅ | ∅ |
3 | ∅ | 264 → 363 → 264 | |
4 | ∅ | 3054 → 5052 → 5232 → 3054 | |
5 | ∅ | 40653 → 61641 → 54612 → 52632 → 42633 → 40653 | |
6 | ∅ | 320544 → 520542 → 531432 → 416643 → 526632 → 441423 → 320544 | |
7 | ∅ | 5306532 → 6316431 → 5506512 → 6426321 → 5416422 → 5316432 → 5306532 | |
8 | ∅ | 42166443 → 54066522 → 64305321 → 64166421 → 55366212 → 54314322 → 42166443 | |
9 | ∅ | 530666532 → 643164321 → 552065412 → 643263321 → 541666422 → 544065222 → 633164331 → 550666512 → 653666211 → 554263212 → 533164332 → 530666532 | |
10 | ∅ |
5316666432 → 5433053322 → 5316666432 5431055322 → 5431664322 → 5431055322 5440665222 → 6432663321 → 5440665222 | |
8 | 2 | 25 | 07 → 61 → 43 → 07[note 1] |
3 | 374 | ∅ | |
4 | ∅ |
1776 → 6062 → 6332 → 3774 → 4244 → 1776 3065 → 6152 → 5243 → 3065 | |
5 | ∅ |
42744 → 47773 → 42744 51753 → 61752 → 63732 → 52743 → 51753 | |
6 | 437734, 640632 | 310665 → 651522 → 532443 → 310665 | |
7 | 6417532 | 5407633 → 7317541 → 6617512 → 6537322 → 5417533 → 6217552 → 6427432 → 5407633
6407632 → 7427431 → 6507622 → 7437331 → 6407632 | |
8 | 75306421 | 31106665 → 65515222 → 53324443 → 31106665
64106632 → 65406322 → 64306432 → 64106632 | |
9 | 443777334 | 543076433 → 732076541 → 764175311 → 665175212 → 654274322 → 543076433
644076332 → 743076431 → 763076411 → 765274211 → 664274312 → 644076332 651076622 → 754373321 → 652076522 → 744274331 → 651076622 | |
10 | 7753064201 | 3111066665 → 6555152222 → 5333244443 → 3111066665
6411066632 → 6554063222 → 6433064432 → 6411066632 6431066432 → 6541066322 → 6543064322 → 6431066432 7531066421 → 7553064221 → 7533064421 → 7531066421 | |
9 | 2 | ∅ | 17 → 53 → 17 |
3 | ∅ | 385 → 484 → 385 | |
4 | ∅ |
3076 → 7252 → 5254 → 3076 5074 → 7072 → 7432 → 5074 | |
5 | 62853 |
52854 → 60873 → 83841 → 74832 → 63843 → 52854 | |
6 | ∅ |
308876 → 850731 → 861621 → 753432 → 520764 → 740742 → 748832 → 652533 → 421665 → 540744 → 708872 → 858821 → 762522 → 542544 → 308876 | |
7 | ∅ |
6518633 → 7328552 → 6518633 | |
8 | 65288533 |
65407433 → 73188652 → 76407422 → 75388432 → 65407433 | |
9 | 753186532 |
641888643 → 754186432 → 753087532 → 854186431 → 773087512 → 865384321 → 763186522 → 754285432 → 652087633 → 853285531 → 762186622 → 754384432 → 641888643 | |
10 | 6632885523 |
5288888854 → 6432885543 → 6531077533 → 7632166522 → 6444164443 → 5288888854 6441886443 → 7521886632 → 7653075322 → 7542166432 → 6441886443 | |
10[4] | 2 | ∅ | 09 → 81 → 63 → 27 → 45 → 09[note 1] |
3 | 495 | ∅ | |
4 | 6174 | ∅ | |
5 | ∅ |
53955 → 59994 → 53955 61974 → 82962 → 75933 → 63954 → 61974 62964 → 71973 → 83952 → 74943 → 62964 | |
6 | 549945, 631764 | 420876 → 851742 → 750843 → 840852 → 860832 → 862632 → 642654 → 420876 | |
7 | ∅ | 7509843 → 9529641 → 8719722 → 8649432 → 7519743 → 8429652 → 7619733 → 8439552 → 7509843 | |
8 | 63317664, 97508421 | 43208766 → 85317642 → 75308643 → 84308652 → 86308632 → 86326632 → 64326654 → 43208766
64308654 → 83208762 → 86526432 → 64308654 | |
9 | 554999445, 864197532 |
865296432 → 763197633 → 844296552 → 762098733 → 964395531 → 863098632 → 965296431 → 873197622 → 865395432 →753098643 → 954197541 → 883098612 → 976494321 → 874197522 → 865296432 | |
10 | 6333176664, 9753086421, 9975084201 | 8655264432 → 6431088654 → 8732087622 → 8655264432
8653266432 → 6433086654 → 8332087662 → 8653266432 8765264322 → 6543086544 → 8321088762 → 8765264322 8633086632 → 8633266632 → 6433266654 → 4332087666 → 8533176642 → 7533086643 → 8433086652 → 8633086632 9775084221 → 9755084421 → 9751088421 → 9775084221 | |
11 | 2 | 37 | ∅ |
3 | ∅ | 4A6 → 5A5 → 4A6 | |
4 | ∅ |
3098 → 9452 → 7094 → 9272 → 7454 → 3098 5096 → 9092 → 9632 → 7274 → 5276 → 5096 | |
5 | ∅ |
72A74 → 82A73 → 84A53 → 73A64 → 72A74 | |
6 | ∅ |
531876 → 740964 → 931872 → 863643 → 531876 | |
7 | ∅ |
631A875 → 951A852 → 972A732 → 873A633 → 753A654 → 730A974 → A62A741 → 982A722 → 875A433 → 752A754 → 831A873 → 954A552 → 84AAA53 → 764A544 → 631A875 751A854 → 941A862 → 973A632 → 863A643 → 751A854 | |
8 | 74318764 |
53218876 → 76409644 → 93218872 → 86636443 → 53218876 75218854 → 762AA744 → 86309743 → 95418652 → 861AA843 → 97418632 → 86418643 → 75218854 | |
9 | 9751A8532 |
871AAA833 → 9770A9332 → A763A6431 → 9741A8632 → 9752A7532 → 8741A8633 → 9552A7552 → 871AAA833 | |
10 | ∅ |
5322188876 → 7664096444 → 9322188872 → 8666364443 → 5322188876 7432188764 → 7643187644 → 7432188764 7631AA8744 → 9743097632 → 9744186632 → 8642188643 → 7652188544 → 7631AA8744 8621AA8843 → 9854186522 → 8661AA8443 → 9743AA6632 → 8762AA7433 → 8753097533 → 9543AA6652 → 8751099533 → 9853AA6522 → 8863097423 → 9654186542 → 8621AA8843 | |
12 | 2 | ∅ | 0B → A1 → 83 → 47 → 29 → 65 → 0B[note 1] |
3 | 5B6 | ∅ | |
4 | ∅ |
3BB8 → 8284 → 6376 → 3BB8 4198 → 8374 → 5287 → 6196 → 7BB4 → 7375 → 4198 | |
5 | 83B74 | 64B66 → 6BBB5 → 64B66 | |
6 | 65BB56 | 420A98 → A73742 → 842874 → 642876 → 62BB86 → 951963 → 860A54 → A40A72 → A82832 → 864654 → 420A98 | |
7 | 962B853 | 841B974 → A53B762 → 971B943 → A64B652 → 960BA53 → B73B741 → A82B832 → 984B633 → 863B754 → 841B974 | |
8 | 873BB744, A850A632 | 4210AA98 → A9737422 → 87428744 → 64328876 → 652BB866 → 961BB953 → A8428732 → 86528654 → 6410AA76 → A92BB822 → 9980A323 → A7646542 → 8320A984 → A7537642 → 8430A874 → A5428762 → 8630A854 → A540X762 → A830A832 → A8546632 → 8520A964 → A740A742 → A8328832 → 86546654 | |
9 | 665BBB556 |
8620BA954 → B852B8631 → A952B8622 → 9872B8433 → 9653B7653 → 8620BA954 8640BA754 → B641B9751 → AA51B9612 → A983B7322 → 9864B6533 → 8640BA754 9630BA853 → B762B8541 → A941B9722 → A874B6432 → 9742B8743 → 9642B8753 → 9641B9753 → A651B9652 → A840BA732 → B873B7431 → A852B8632 → 9852B8633 → 9652B8653 → 9630BA853 | |
10 | 8843BB7734 |
64310AA876 → A952BB8622 → 9984197323 → 8765286544 → 64310AA876 87650A6544 → A4310AA872 → A985286322 → 87650A6544 A8510AA632 → A9850A6322 → A8750A6432 → A8530A8632 → A8550A6632 → A8510AA632 | |
13 | 2 | ∅ | 1B → 93 → 57 → 1B |
3 | ∅ | 5C7 → 6C6 → 5C7 | |
4 | ∅ |
30BA → B652 → 90B4 → B472 → 9294 → 7476 → 30BA 50B8 → B292 → 9654 → 50B8 5298 → 7296 → 70B6 → B0B2 → B832 → 9474 → 5298 | |
5 | ∅ | 83C85 → 92C94 → A4C73 → 95C64 → 83C85 | |
6 | 951A74 | ∅ | |
7 | ∅ |
951CA74 → B63C862 → A81CA43 → B75C652 → A61CA63 → B73C852 → A82C943 → A74C753 → 961CA64 → B62C962 → A92C933 → A75C653 → 951CA74 972C954 → A53C873 → 972C954 A71CA53 → B74C752 → A71CA53 | |
8 | 9541A874 |
641CCA87 → B840B842 → B9438832 → 96538764 → 641CCA87 | |
9 | ∅ |
9620CBA64 → C962C9631 → BA62C9622 → A982C9433 → A764C7653 → 9620CBA64 A751CA753 → B751CA752 → B951CA732 → B973C8532 → A862C9643 → A751CA753 A861CA643 → B761CA652 → B950CB732 → C983C8431 → B963C8632 → A861CA643 | |
10 | 95441A8874 |
86661A6665 → 92CCCCCC94 → A832CC9943 → A9750B7533 → B7621AA652 → A881CCA443 → B965CC6632 → A962CC9633 → A973299533 → 86661A6665 99650B7634 → B651CCA762 → BA640B8622 → B983CC8432 → A984CC7433 → 99650B7634 A8630B9643 → B763CC8652 → A9620BA633 → B875CC6542 → A8630B9643 | |
14 | 2 | 49 |
2B → 85 → 2B 0D → C1 → A3 → 67 → 0D[note 1] |
3 | 6D7 | ∅ | |
4 | ∅ | 61B8 → A1B4 → A574 → 61B8 | |
5 | ∅ |
75D77 → 7DDD6 → 75D77 93D95 → A3D94 → A5D74 → 94D85 → 93D95 | |
6 | 76DD67 |
530CA9 → C73962 → A60C74 → C60C72 → CA0C32 → CA6632 → A6DD64 → 973965 → 640C98 → C51B82 → B92A43 → 974865 → 530CA9 A40C94 → C64872 → A40C94 A80C54 → C62A72 → A80C54 | |
7 | ∅ | A62DA74 → B63D973 → A82DA54 → B64D873 → A71DB64 → C73D962 → B92DA43 → B85D753 → A62DA74 | |
8 | CA70C632 |
7550C887 → C32DDAA2 → BB8DD423 → BA72A633 → 9770C665 → C410CC92 → CBA48322 → A9739644 → 7550C887 A410CC94 → CB648722 → A940C944 → C6548872 → A430CA94 → C7648762 → A410CC94 A810CC54 → CB62A722 → A980C544 → C652A872 → A830CA54 → C762A762 → A810CC54 | |
9 | 776DDD667, B852DA853 | A731DBA64 → C863D9752 → B941DB943 → C874D8652 → B831DBA53 → C884D8552 → B832DAA53 → B874D8653 → A731DBA64 | |
15 | 2 | ∅ | ∅ |
3 | ∅ | 6E8 → 7E7 → 6E8 | |
5 | A4E95 | ∅ | |
6 | ∅ |
41EECB → DA0D42 → DB5832 → B82B64 → 971C76 → B2EEB4 → C9EE43 → BA2B44 → 975876 → 41EECB 960D86 → D31CB2 → CA7643 → 960D86 | |
7 | ∅ |
A62EB85 → C63EA83 → B93EA54 → B74E974 → A71EC75 → D72EB72 → CB3EA33 → B97E654 → A62EB85 B70ED74 → E93EA51 → DB4E932 → CA6E743 → B83EA64 → B73EA74 → B72EB74 → C73EA73 → B92EB54 → C75E873 → B70ED74 | |
8 | A94EE955 | 8640DA87 → D620DC82 → 8640DA87 | |
9 | C962EB853 |
A742EBA75 → C752EB973 → C961EC853 → D972EB752 → CB61EC833 → D994E9552 → C943EAA53 → B964E9854 → A742EBA75 B862EB864 → C751EC973 → D971EC752 → DB71EC732 → DB93EA532 → CA84E9643 → B862EB864 | |
10 | AA54EE9945 |
96660D8886 → D3221CCCB2 → CAAA764443 → 96660D8886 B761EEC874 → DA640DA842 → DB661C8832 → CA821CC643 → BA961C8544 → B7641CA874 → B761EEC874 | |
16[5] | 2 | ∅ |
2D → A5 → 4B → 69 → 2D 0F → E1 → C3 → 87 → 0F[note 1] |
3 | 7F8 | ∅ | |
4 | ∅ |
3FFC → C2C4 → A776 → 3FFC A596 → 52CB → A596 E0E2 → EB32 → C774 → 7FF8 → 8688 → 1FFE → E0E2 E952 → C3B4 → 9687 → 30ED → E952 | |
5 | ∅ |
86F88 → 8FFF7 → 86F88 A3FB6 → C4FA4 → B7F75 → A3FB6 A4FA6 → B3FB5 → C5F94 → B6F85 → A4FA6 | |
6 | 87FF78 |
310EED → ED9522 → CB3B44 → 976887 → 310EED 532CCB → A95966 → 532CCB 840EB8 → E6FF82 → D95963 → A42CB6 → A73B86 → 840EB8 A80E76 → E40EB2 → EC6832 → C91D64 → C82C74 → A80E76 C60E94 → E82C72 → CA0E54 → E84A72 → C60E94 | |
7 | C83FB74 |
B62FC95 → D74FA83 → C92FC64 → D85F973 → C81FD74 → E94FA62 → DA3FB53 → CA5F954 → B74FA85 → B62FC95 B71FD85 → E83FB72 → DB3FB43 → CA6F854 → B73FB85 → C63FB94 → C84FA74 → B82FC75 → D73FB83 → CA3FB54 → C85F974 → B71FD85 | |
8 | ∅ |
3110EEED → EDD95222 → CBB3B444 → 97768887 → 3110EEED 5332CCCB → A9959666 → 5332CCCB 7530ECA9 → E951DA62 → DB52CA43 → B974A865 → 7530ECA9 A832CC76 → A940EB66 → E742CB82 → CA70E854 → E850EA72 → EC50EA32 → EC94A632 → C962C964 → A832CC76 C610EE94 → ED82C722 → CBA0E544 → E874A872 → C610EE94 C630EC94 → E982C762 → CA30EC54 → E984A762 → C630EC94 C650EA94 → E852CA72 → CA50EA54 → E854AA72 → C650EA94 CA10EE54 → ED84A722 → CB60E944 → E872C872 → CA10EE54 | |
9 | 887FFF778, DA72FC853 |
C851FDA74 → E972FC862 → DC61FD933 → EAA5F9552 → D954FAA63 → C953FBA64 → C863FB974 → C851FDA74 C862FC974 → D861FD973 → EA71FD852 → EC82FC732 → DC94FA633 → CA83FB754 → C862FC974 CA40FEB54 → FA85F9751 → EA51FDA52 → EC84FA732 → DB82FC743 → DA83FB753 → CA62FC954 → D873FB873 → CA40FEB54 |
Kaprekar's constants in base 10
[edit]Numbers of length four digits
[edit]In 1949 D. R. Kaprekar discovered[6] that if the above process is applied to four-digit numbers in base 10, the sequence converges to 6174 within seven iterations or, more rarely, converges to 0. The number 6174 is the first Kaprekar's constant to be discovered, and thus is sometimes known as Kaprekar's constant.[7][8][9]
The set of numbers that converge to zero depends on whether leading zeros are discarded (the usual formulation) or are retained (as in Kaprekar's original formulation). In the usual formulation, there are 77 four-digit numbers that converge to zero,[10] for example 2111. However, in Kaprekar's original formulation the leading zeros are retained, and only repdigits such as 1111 or 2222 map to zero. This contrast is illustrated below:
discard leading zeros | retain leading zeros |
---|---|
2111 − 1112 = 999 |
2111 − 1112 = 0999 |
Below is a flowchart. Leading zeros are retained, however the only difference when leading zeros are discarded is that instead of 0999 connecting to 8991, we get 999 connecting to 0.
![](http://upload.wikimedia.org/wikipedia/commons/thumb/4/4f/KaprekarRoutineFlowGraph6174.svg/770px-KaprekarRoutineFlowGraph6174.svg.png)
Numbers of length three digits
[edit]If the Kaprekar routine is applied to numbers of three digits in base 10, the resulting sequence will almost always converge to the value 495 in at most six iterations, except for a small set of initial numbers which converge instead to 0.[7]
The set of numbers that converge to zero depends on whether leading zeros are discarded (the usual formulation) or are retained (as in Kaprekar's original formulation). In the usual formulation, there are 60 three-digit numbers that converge to zero,[11] for example 211. However, in Kaprekar's original formulation the leading zeros are retained, and only repdigits such as 111 or 222 map to zero.
Below is a flowchart. Leading zeros are retained, however the only difference when leading zeros are discarded is that instead of 099 connecting to 891, we get 99 connecting to 0.
![](http://upload.wikimedia.org/wikipedia/commons/thumb/2/2d/KaprekarRoutineFlowGraph495.svg/750px-KaprekarRoutineFlowGraph495.svg.png)
Other digit lengths
[edit]For digit lengths other than three or four (in base 10), the routine may terminate at one of several fixed points or may enter one of several cycles instead, depending on the starting value of the sequence.[7] See the table in the section above for base 10 fixed points and cycles.
The number of cycles increases rapidly with larger digit lengths, and all but a small handful of these cycles are of length three. For example, for 20-digit numbers in base 10, there are fourteen constants (cycles of length one) and ninety-six cycles of length greater than one, all but two of which are of length three. Odd digit lengths produce fewer different end results than even digit lengths.[12][13]
Sometimes these numbers (495, 6174, and their counterparts in other digit lengths or in bases other than 10) are called "Peyush constants" named after Peyush Dixit who solved this routine as a part of his IMO 2000 (International Mathematical Olympiad, Year 2000) thesis. [14]
Programming example
[edit]The example below implements the Kaprekar mapping described in the definition above to search for Kaprekar's constants and cycles in Python.
import string
from collections import deque
from collections.abc import Sequence, Generator
BASE36_DIGITS = f"{string.digits}{string.ascii_uppercase}"
def digit_count(x: int, /, base: int = 10) -> int:
count = 0
while x > 0:
count += 1
x //= base
return count
def get_digits(x: int, /, base: int = 10, init_k: int = 0) -> str:
if init_k > 0:
k = digit_count(x, base)
digits = deque()
while x > 0:
digits.appendleft(BASE36_DIGITS[x % base])
x //= base
if init_k > 0:
for _ in range(k, init_k):
digits.appendleft("0")
return "".join(digits)
def kaprekar_map(x: int, /, base: int = 10, init_k: int = 0) -> int:
digits = "".join(sorted(get_digits(x, base, init_k)))
descending = int("".join(reversed(digits)), base)
ascending = int(digits, base)
return descending - ascending
def kaprekar_cycle(
x: int | str | bytes | bytearray, /, base: int = 10
) -> list[int | str]:
"""
Return Kaprekar's cycles as list
>>> kaprekar_cycle(8991)
[6174]
>>> kaprekar_cycle(865296432)
[865296432, 763197633, 844296552, 762098733, 964395531, 863098632, 965296431, 873197622, 865395432, 753098643, 954197541, 883098612, 976494321, 874197522]
>>> kaprekar_cycle('09')
[9, 81, 63, 27, 45]
>>> kaprekar_cycle('0F', 16)
['0F', 'E1', 'C3', '87']
>>> kaprekar_cycle('B71FD85', 16)
['B71FD85', 'E83FB72', 'DB3FB43', 'CA6F854', 'B73FB85', 'C63FB94', 'C84FA74', 'B82FC75', 'D73FB83', 'CA3FB54', 'C85F974']
"""
leading_zeroes_retained = not isinstance(x, int)
init_k = len(x) if leading_zeroes_retained else 0
x = int(x) if base == 10 else int(x, base)
seen = []
while x not in seen:
seen.append(x)
x = kaprekar_map(x, base, init_k)
cycle = []
while x not in cycle:
cycle.append(x)
x = kaprekar_map(x, base, init_k)
return cycle if base == 10 else [get_digits(x, base, init_k) for x in cycle]
if __name__ == "__main__":
import doctest
doctest.testmod()
See also
[edit]- Arithmetic dynamics
- Collatz conjecture
- Dudeney number
- Factorion
- Happy number
- Kaprekar number
- Meertens number
- Narcissistic number
- Perfect digit-to-digit invariant
- Perfect digital invariant
- Sum-product number
- Sorting algorithm
Citations
[edit]- ^ Hanover 2017, p. 1, Overview.
- ^ Hanover 2017, p. 3, Methodology.
- ^ (sequence A099009 in the OEIS)
- ^ "Sample Kaprekar Series".
- ^ "Sample Kaprekar Series for hexadecimal numbers".
- ^ Kaprekar DR (1955). "An Interesting Property of the Number 6174". Scripta Mathematica. 15: 244–245.
- ^ a b c Weisstein, Eric W. "Kaprekar Routine". MathWorld.
- ^ Yutaka Nishiyama, Mysterious number 6174
- ^ Kaprekar DR (1980). "On Kaprekar Numbers". Journal of Recreational Mathematics. 13 (2): 81–82.
- ^ (sequence A069746 in the OEIS)
- ^ (sequence A090429 in the OEIS)
- ^ "Sample Kaprekar Series".
- ^ "Playing with Numbers".
- ^ Hanover 2017, p. 14, Operations.
References
[edit]- Hanover, Daniel (2017). "The Base Dependent Behavior of Kaprekar's Routine: A Theoretical and Computational Study Revealing New Regularities". International Journal of Pure and Applied Mathematics. arXiv:1710.06308.
External links
[edit]![](http://upload.wikimedia.org/wikipedia/en/thumb/4/4a/Commons-logo.svg/30px-Commons-logo.svg.png)
- Bowley, Roger (5 December 2011). "6174 is Kaprekar's Constant". Numberphile. University of Nottingham: Brady Haran. Retrieved 2024-01-17.
- Working link to YouTube
- Sample (Perl) code to walk any four-digit number to Kaprekar's Constant
- Sample (Python) code to walk any four-digit number to Kaprekar's Constant