6174 is the Kaprekar constant. All natural numbers less than 10,000 (except some with same digits) can be reduced to 6174 in the following steps:
Order the digits of the number in descending and ascending order and compute the difference. Repeat the process till you get 6174.
Example
n = 2376
Digits in descending order = 7632
Digits in ascending order = 2367
Step1:
>> 7632 - 2367
ans = 5265
Step 2:
>> 6552 - 2556
ans = 3996
Step3:
>> 9963 - 3699
ans = 6264
Step4
>> 6642 - 2466
ans = 4176
Step5
>> 7641 - 1467
ans = 6174
Total number of steps = 5.
Your function should return the number of Kaprekar steps for a given input. Numbers such as 2222 will end in zero. These numbers will never result in 6174. They should return Inf.
Solution Stats
Problem Comments
18 Comments
Solution Comments
Show comments
Loading...
Problem Recent Solvers2371
Suggested Problems
-
Project Euler: Problem 5, Smallest multiple
1648 Solvers
-
1498 Solvers
-
How many trades represent all the profit?
615 Solvers
-
607 Solvers
-
674 Solvers
More from this Author96
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
The behavior at x=6174 is artifically set to 0, which taints the pure-recursive solution, but hey it was cool problem!
The K constant is 495 for 3 digits.
So the test with 691 is wrong.
Why tests with only one digit ?
Did I miss something ?
I don't understand the Test Suite for x = 3 and x = 1 too. what should I do in this case?
For x=3, the steps are 3000-0003=2997, 9972-2799=7173, etc.
Problem description is confusing as there are different Kaprekar constants depending on the number of digits. [0 9 495 6174 for 1, 2,3 4 digits respectively.
getting this error:
Internal Server Error - Read
The server encountered an internal error or misconfiguration and was unable to complete your request.
Reference #3.c2c1ab8.1412090777.18623697
any ideas?
The problem should specify that any number with less than four digits should be filled up to four digits with leading zeros. (e.g. 3 -> 0003)
Very nice and interesting problem!
I like the recursion aspect of this problem.
There is a small correction needed in the problem statement. Not all natural numbers, but 4 digit numbers can be reduced to Kaprekar number by the mentioned method. Similarly 3 digit numbers can be reduced to 495
https://en.wikipedia.org/wiki/D._R._Kaprekar
How it works x = 1?????
For those confused with test cases 2,3 and 5, like myself before, do conversion to 4-digit integer. Here is an example:
x = 1:
1000-0001 = 999
9990-0999 = 8991
9981-1899 = 8082
8820-0288 = 8532
8532-2358 = 6174
Therefore, y_correct = 5
love it!!!
Very nice. Took some few minutes to crack this.
What I did is to convert x into string and then use sort function.
nice problem
ATTENTION
This problem statement is poorly given and leaves out a crucial element- the input number must always have 4 digits, so if 1 is the input, the first iteration should be 1000-0001. the second iteration should be 9990-0999. for the problem as stated, the test suite gives solutions that are incorrect.
Quite interesting!
interesting problem