The behavior at x=6174 is artifically set to 0, which taints the pure-recursive solution, but hey it was cool problem!
The K constant is 495 for 3 digits.
So the test with 691 is wrong.
Why tests with only one digit ?
Did I miss something ?
I don't understand the Test Suite for x = 3 and x = 1 too. what should I do in this case?
For x=3, the steps are 3000-0003=2997, 9972-2799=7173, etc.
Problem description is confusing as there are different Kaprekar constants depending on the number of digits. [0 9 495 6174 for 1, 2,3 4 digits respectively.
getting this error:
Internal Server Error - Read
The server encountered an internal error or misconfiguration and was unable to complete your request.
Reference #3.c2c1ab8.1412090777.18623697
any ideas?
The problem should specify that any number with less than four digits should be filled up to four digits with leading zeros. (e.g. 3 -> 0003)
Very nice and interesting problem!
I like the recursion aspect of this problem.
There is a small correction needed in the problem statement. Not all natural numbers, but 4 digit numbers can be reduced to Kaprekar number by the mentioned method. Similarly 3 digit numbers can be reduced to 495
https://en.wikipedia.org/wiki/D._R._Kaprekar
How it works x = 1？？？？？
For those confused with test cases 2,3 and 5, like myself before, do conversion to 4-digit integer. Here is an example:
x = 1:
1000-0001 = 999
9990-0999 = 8991
9981-1899 = 8082
8820-0288 = 8532
8532-2358 = 6174
Therefore, y_correct = 5
love it!!!
Very nice. Took some few minutes to crack this.
What I did is to convert x into string and then use sort function.
function y = KaprekarSteps(x)
y=0;
while x<1000
x=x*10;
end
x1=floor(x/1000);
x2=floor((x-x1*1000)/100);
x3=floor((x-x1*1000-x2*100)/10);
x4=mod(x,10);
if x1==x2&&x2==x3&&x3==x4
y=Inf;
elseif x==6174
y=0;
else
x_ori=[x1,x2,x3,x4];
x_ori=sort(x_ori);
x_inv=fliplr(x_ori);
dif=0;
while dif ~=6174
x_1=1000*x_inv(1)+100*x_inv(2)+10*x_inv(3)+x_inv(4);
x_2=1000*x_ori(1)+100*x_ori(2)+10*x_ori(3)+x_ori(4);
dif=x_1-x_2;
y=y+1;
while dif<1000
dif=dif*10;
end
dif1=floor(dif/1000);
dif2=floor((dif-dif1*1000)/100);
dif3=floor((dif-dif1*1000-dif2*100)/10);
dif4=mod(dif,10);
dif_ori=[dif1,dif2,dif3,dif4];
x_ori=sort(dif_ori);
x_inv=fliplr(x_ori);
end
end
end
The test suite doesn't match the problem description - how can the answer to test two be 5?
Cheated with 1...:
1000 - 0001 = 999.
999 - 999 = 0
y = inf;
No?
How it works with x = 3, x = 691, x = 1?
This works for all but test 3 where it gives, in my opinion, the correct answer 8.
But zero is not sorted in this solution. You should get same answer for input x = 691 and 6910. (And then no need for abs())
1) 9610-0169=9441
2) 9441-1449=7992
3) 9972-2799=7173
4) 7731-1377=6354
5) 6543-3456=3087
6) 8730-0378=8352
7) 8532-2358=6174
This has been the lamest test so far. You need to pad the numbers with zeros to build it up to be a 4 digit number. Not explained in the rules.
Not all test cases seem to be correct. For example x=3 would imply that the next value should be x=3-3=0, so y_correct=Inf and not 6
An efficient lookup table solution
Interesting - a recursive approach
Could you stop doing this kind of thing? I think it would be a lot more fun if we could see the _actual_ best solution..
Find the numeric mean of the prime numbers in a matrix.
6592 Solvers
1359 Solvers
662 Solvers
Arrange Vector in descending order
3332 Solvers
Calculate Amount of Cake Frosting
5911 Solvers
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!