How do you get to Keprekar's constant when you start with a one digit number?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Walter Roberson
2021-5-26
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 个评论
Walter Roberson
2021-5-26
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.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!