6174 is the Kaprekar Constant. This number is special as we always get this number when following steps are followed for any four digit number such that all digits of number are not same, i.e., all four digit numbers excluding (0000, 1111, …)
- Sort four digits in ascending order and store result in a number “asc”.
- Sort four digits in descending order and store result in a number “desc”.
- Subtract number larger number from smaller number, i.e., abs(asc – desc).
- Repeat above three steps until the result of subtraction doesn’t become equal to the previous number.
We always end up with 6174.
Following is the program to demonstrate the same.
6174 6174 6174
n = 2324 1) asc = 2234 2) desc = 4322 3) Difference = 2088 4) Repeating above steps as difference is not same as n n = 2088 1) asc = 0288 2) desc = 8820 3) Difference = 8532 4) Repeating above steps as difference is not same as n. n = 8532 1) asc = 2358 2) desc = 8532 3) Difference = 6174 4) Repeating above steps as difference is not same as n. n = 6174 1) asc = 1467 2) desc = 7641 3) Difference = 6174 Stopping here as difference is same as n.
This article is contributed by Gaurav Saxena. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Kaprekar Number
- Find Index of given fibonacci number in constant time
- Tau - A Mathematical Constant
- Percentage increase in the cylinder if the height is increased by given percentage but radius remains constant
- Maximize the first element of the array such that average remains constant
- Maximum sum of elements in a diagonal parallel to the main diagonal of a given Matrix
- Minimize cost of choosing and skipping array elements to reach end of the given array
- Count positions in a chessboard that can be visited by the Queen which are not visited by the King
- Print first K distinct Moran numbers from a given array
- Count three-digit numbers having difference X with its reverse
- Maximum XOR of a path from top-left to bottom-right cell of given Matrix
- Maximize count of pairs (i, j) from two arrays having element from first array not exceeding that from second array
- Print X array elements closest to the Kth smallest element in the array
- XOR of array elements whose modular inverse with a given number exists
Improved By : Mithun Kumar