Difference between revisions of "Kaprekars Constant"
Jump to navigation
Jump to search
| Line 17: | Line 17: | ||
function kc(x) | function kc(x) | ||
{ | { | ||
| − | k= | + | k=x.⁋.⪪.⋰; |
| − | kp:= | + | kp:=k.⋱.⚯*1 -k.⋰.⚯; |
_y=k; | _y=k; | ||
| − | var r=-1 | + | var r=-1, rt=-1,rs=[]; |
| − | |||
| − | |||
try | try | ||
{ | { | ||
| Line 31: | Line 29: | ||
if(t==6174 && r==-1) | if(t==6174 && r==-1) | ||
{ | { | ||
| − | r=i; | + | r=i;rt=t;rs.push(t); |
| − | |||
| − | |||
throw("EXIT") | throw("EXIT") | ||
} | } | ||
| Line 40: | Line 36: | ||
rs.push(t) | rs.push(t) | ||
} | } | ||
| − | _y= | + | _y=t.⁋.⪪; |
| − | |||
} | } | ||
); | ); | ||
Revision as of 15:40, 4 September 2024
Kaprekar's constant
The number 6174 is known as Kaprekar's constant[| Kaprekar's Constant 6174] after the Indian mathematician D. R. Kaprekar. This number is renowned for the following rule:
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.
Video: https://www.youtube.com/watch?v=xtyNuOikdE4
z^3 Solution
1001..1010@kc;
function kc(x)
{
k=x.⁋.⪪.⋰;
kp:=k.⋱.⚯*1 -k.⋰.⚯;
_y=k;
var r=-1, rt=-1,rs=[];
try
{
(1..7)@(
function(i)
{
var t=kp(_y)
if(t==6174 && r==-1)
{
r=i;rt=t;rs.push(t);
throw("EXIT")
}
else
{
rs.push(t)
}
_y=t.⁋.⪪;
}
);
}
catch(err)
{
}
⊫([x,r,rt,rs])
return([x,r,_y,rs])
}
Answer:
|
|
|
|
|
|
|
|
|
|
| x | kc | |||||||||||||
| 1001 |
| |||||||||||||
| 1002 |
| |||||||||||||
| 1003 |
| |||||||||||||
| 1004 |
| |||||||||||||
| 1005 |
| |||||||||||||
| 1006 |
| |||||||||||||
| 1007 |
| |||||||||||||
| 1008 |
| |||||||||||||
| 1009 |
| |||||||||||||
| 1010 |
|