How do you get to Keprekar's constant when you start with a one digit number?
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Ntandoyakhe Tshuma
le 26 Mai 2021
Commenté : Walter Roberson
le 26 Mai 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 commentaires
Réponse acceptée
Walter Roberson
le 26 Mai 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 commentaires
Walter Roberson
le 26 Mai 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.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur MATLAB Report Generator dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!