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
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Kaprekar Number
- Tau - A Mathematical Constant
- Maximize the first element of the array such that average remains constant
- Find Index of given fibonacci number in constant time
- Percentage increase in the cylinder if the height is increased by given percentage but radius remains constant
- XOR of a subarray (range of elements) | Set 2
- Maximize the numbers of splits in an Array having sum divisible by 3
- Find the angle of Rotational Symmetry of an N-sided regular polygon
- Largest number made up of X and Y with count of X divisible by Y and of Y by X
- Count quadruplets(A, B, C, D) till N such that sum of square of A and B is equal to that of C and D
- Minimum count of numbers required with unit digit X that sums up to N
- Sum of all divisors from 1 to N | Set 3
- Check if a graph is Strongly, Unilaterally or Weakly connected
- Represent K as sum of N-bonacci numbers
Improved By : Mithun Kumar