+ 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! :)
47 Answers
+ 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
+ 13
That was my first code here at SoloLearn... a year ago :)
https://code.sololearn.com/cXQ4gS5Gww8z/?ref=app
https://code.sololearn.com/cRqDeh0LFCLy/?ref=app
https://code.sololearn.com/cv426gaf4R5E/?ref=app
+ 13
Thanks to all for suggestions. My code has been corrected now 😊
+ 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.
+ 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
+ 8
Thanks for the challenge 👍
Here's my try :
https://code.sololearn.com/cW1dvRU5RVB1/?ref=app
+ 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 :)
+ 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
+ 6
Here's my solution:
https://code.sololearn.com/WWKvC3m62ZHj/?ref=app
+ 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
+ 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.
+ 4
@Kuba, I was waiting your answer because I know it was only you have Kaprekar's constant codes shared :)
+ 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.
+ 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))
+ 4
@Codasan, I am waiting your code. Try on Sayan, André, Antonie and LukArToDo's code. Their solutions are giving right answer
+ 3
This is my code.
https://code.sololearn.com/cJ6C13IBaeO8
+ 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.
+ 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."
+ 3
Here you go. Checks for valid input
https://code.sololearn.com/c470T34FB40j/?ref=app