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 email@example.com. 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
- Minimize difference between maximum and minimum array elements by exactly K removals
- Split squares of first N natural numbers into two sets with minimum absolute difference of their sums
- Remove characters from given string whose frequencies are a Prime Number
- Count clockwise array rotations required to maximize count of array elements present at indices same as their value
- Check if GCD of all Composite Numbers in an array divisible by K is a Fibonacci Number or not
- Construct smallest N-digit number possible by placing digits at positions specified by given array
- Maximize minimum of array generated by maximums of same indexed elements of two rows of a given Matrix
- Count subsequences for every array element in which they are the maximum
- Predict the winner of a card game of removing K cards in each turn such that Bitwise AND of K and size of pile is 0
Improved By : Mithun Kumar