How do you get to Keprekar's constant when you start with a one digit number?
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Ntandoyakhe Tshuma
il 26 Mag 2021
Commentato: Walter Roberson
il 26 Mag 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 Commenti
Risposta accettata
Walter Roberson
il 26 Mag 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 Commenti
Walter Roberson
il 26 Mag 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.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su MATLAB Report Generator in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!