+ 20

🏆🏆Challenge [Kaprekar's Constant]🏆🏆

Take any four digit number and do the following: Rearrange the string of digits to form the largest and smallest 4-digit numbers possible. Take these two numbers and subtract the smaller number from the larger. Count also how many iterations loop gets. Suppose we choose the number 8082. 8820−0288=8532 8532−2358=6174 7641−1467=6174 It hits 6174 and then stops. Info: https://www.math.hmc.edu/funfacts/ffiles/10002.5-8.shtml https://plus.maths.org/content/mysterious-number-6174 Any languages! Good luck! :)

2nd Nov 2017, 6:10 PM
Ferhat Sevim
Ferhat Sevim - avatar
47 RĂ©ponses
+ 17
Here's my C# implementation! ✌ I've been very busy lately and regret for less active in challenge. Anyway here you go, enjoy~ ❀ https://code.sololearn.com/cbdD9vrE54yP/?ref=app
3rd Nov 2017, 10:08 AM
Zephyr Koo
Zephyr Koo - avatar
2nd Nov 2017, 8:17 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 13
Thanks to all for suggestions. My code has been corrected now 😊
3rd Nov 2017, 7:10 AM
LukArToDo
LukArToDo - avatar
21st Nov 2017, 3:30 PM
KĂ€zĂź MrĂźdĂčl HĂžssÀßn
KĂ€zĂź MrĂźdĂčl HĂžssÀßn - avatar
+ 10
Very interesting problem and I am going to write some solutions for it. In the meanwhile, I would like to notify participants to this point that they should be careful about numbers containing zero digit(s), for example 1000. I've tried several of the submitted codes and they did not worked properly for 1000.
3rd Nov 2017, 7:20 AM
Vahid Shirbisheh
Vahid Shirbisheh - avatar
+ 9
Here is my answer in Java. I try to reach Kaprekar's Constant for 10 different random numbers in every run of my program. I also consider 3-digits, 2-digits and 1-digits numbers. For example if the random number is 324, then I consider it as 0324 and solve the problem. https://code.sololearn.com/c3Ptas4jU4RF
3rd Nov 2017, 2:08 PM
Vahid Shirbisheh
Vahid Shirbisheh - avatar
+ 8
Thanks for the challenge 👍 Here's my try : https://code.sololearn.com/cW1dvRU5RVB1/?ref=app
3rd Nov 2017, 12:14 AM
LukArToDo
LukArToDo - avatar
+ 7
I remember my satisfaction when I had that code going properly and it worked in both Ruby and Python. Later on, I decided to do some more porting of my codes between Ruby and Python, too :)
2nd Nov 2017, 8:25 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 7
Here is my own answer in Python. You can try any numbers except 1111, 2222 and etc. I hope I can see more in Java, C++ and C-sharp. Thanks for everybody to enjoy challenge! :) https://code.sololearn.com/cMC5yI4nn371/?ref=app https://code.sololearn.com/cFhNezpo2wb1/?ref=app https://code.sololearn.com/ck1896cNQQDA/?ref=app
4th Nov 2017, 12:24 AM
Ferhat Sevim
Ferhat Sevim - avatar
3rd Nov 2017, 4:49 AM
Jonathan Pizarra (JS Challenger)
Jonathan Pizarra (JS Challenger) - avatar
+ 5
heres mine### includes 2 and 3 digits also... for 2 end is 0 for 3 end is 495 for 4 end is 6174 https://code.sololearn.com/cdJpN0RtPNnW/?ref=app
2nd Nov 2017, 7:38 PM
sayan chandra
sayan chandra - avatar
+ 5
@André, you are right, I tested 4555 on others code, couldn't work. You, Sayan and Jonathan have fixed it. I hope they will solve it.
3rd Nov 2017, 6:14 AM
Ferhat Sevim
Ferhat Sevim - avatar
+ 4
@Kuba, I was waiting your answer because I know it was only you have Kaprekar's constant codes shared :)
2nd Nov 2017, 8:21 PM
Ferhat Sevim
Ferhat Sevim - avatar
+ 4
@Sayan, I did and I see it is working. I just felt confused when I saw it was going to 0. Because my code even if I only input 18, it will go to 6174.
2nd Nov 2017, 8:42 PM
Ferhat Sevim
Ferhat Sevim - avatar
+ 4
heres the catch... the right idea is quite a bit diff. theres no such thing as one karpekar const. its a series.. some series has one const some has more than a 1 for a 2 dig num... the series is 9-81-63-27-45-9 for a 3 digit its 495-495 or 0-0 for a four digit..some has 0-0 (eg. 1111,2222) rest has 6174-6174 ((http://kaprekar.sourceforge.net/output/sample.php))
3rd Nov 2017, 5:56 AM
sayan chandra
sayan chandra - avatar
+ 4
@Codasan, I am waiting your code. Try on Sayan, André, Antonie and LukArToDo's code. Their solutions are giving right answer
3rd Nov 2017, 7:59 AM
Ferhat Sevim
Ferhat Sevim - avatar
2nd Nov 2017, 9:47 PM
sneeze
sneeze - avatar
+ 3
@sayan The issue we are referring to is that your code currently doesn't return the correct constant when entering 4555, a four-digit number. I'm aware that there are different constants for different numbers of digits, but Ferhat's original challenge was specifically for four digit ones reaching 6174. It is not that our codes don't work; they're following the directions.
3rd Nov 2017, 5:35 AM
André
André - avatar
+ 3
@Sayan Good find! You are right, but that is normal: the loop is unavoidable for two-digit numbers, for which there is no constant. From the second website Ferhat linked: "...all two digit numbers will reach the loop 9→81→63→27→45→9. Unlike for three and four digit numbers, there is no unique kernel for two digit numbers."
3rd Nov 2017, 5:43 AM
André
André - avatar
+ 3
Here you go. Checks for valid input https://code.sololearn.com/c470T34FB40j/?ref=app
3rd Nov 2017, 3:55 PM
Vari93
Vari93 - avatar