How do you get to Keprekar's constant when you start with a one digit number?
1 view (last 30 days)
Show older comments
Ntandoyakhe Tshuma
on 26 May 2021
Commented: Walter Roberson
on 26 May 2021
How do you get to Keprekar's constant when you start with a one digit number? As far as l know there are 2 values considered as Keprekar's constants 495 (if you start with a 3 digit number) or 6174 if you start with a 4 digit number. I am working on a function to count how many steps are needed to take a number and get it to Keprekars constant using Keprekar's routine described below. I saw a Matlab cody challenge linked here( https://www.mathworks.com/matlabcentral/cody/groups/2/problems/68 ) where one of the test cases says you actually can get to Keprekars constant when you start with one digit.
Keprekar's Routine:
- Take any four-digit number, using at least two different digits (leading zeros are allowed).
- Arrange the digits in descending and then in ascending order to get two four-digit numbers, adding leading zeros if necessary.
- Subtract the smaller number from the bigger number.
- Go back to step 2 and repeat
Source for Keprekars Routine Description:
0 Comments
Accepted Answer
Walter Roberson
on 26 May 2021
new = randi(9)
while true
old = new
new = sprintf('%04d', old)
new = str2num(sort(new, 'descend')) - str2num(sort(new,'ascend'))
if new == old; break; end
end
new
2 Comments
Walter Roberson
on 26 May 2021
Note: the code could be written more efficiently. The point was not to be as efficient as possibe: the point was to show it could be done.
More Answers (0)
See Also
Categories
Find more on Get Started with MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!