I was recently introduced to Kaperkar’s Constant.
It is quite magical. You take any four digit number , sort the digits from highest to lowest to create a new number , sort the digits from lowest to highest to get , and calculate and new number . You repeat this procedure enough times and you end up with .
I made a nifty implementation of that in Julia below.
|# We work out the Kaprekar's constant|
|y=[j for j in "$x"]|
|sum(parse(Int,y[i])*10^(i–1) for i=1:length(y))|
|Finds a fixed point by the repeated appliction of subtracting the largest|
|combination of digits from the smallest.|
|Returns the fixed ponit and flag indicating of one was found or not.|
|(x==x_prev) && break|
|find_fp(2241) # (6174,true)|