Difference between revisions of "Kaprekars Constant"
(10 intermediate revisions by the same user not shown) | |||
Line 2: | Line 2: | ||
=Kaprekar's constant= | =Kaprekar's constant= | ||
− | The number 6174 is known as Kaprekar's constant[ | + | The number 6174 is known as Kaprekar's constant[[https://en.wikipedia.org/wiki/6174 | 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). | Take any four-digit number, using at least two different digits (leading zeros are allowed). | ||
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=[]; |
− | + | (1..8)@( | |
− | + | 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.⁋.⪡; | |
− | + | } | |
− | return([x,r | + | ); |
− | + | ☝; | |
+ | return([x,r,rs]) | ||
} | } | ||
+ | </pre> | ||
− | + | {| style="" id="TABLE95" class="null withtitle wikitable" donotcaption="true" | | |
− | {| style="" id=" | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
|- | |- | ||
| x | | x | ||
Line 382: | Line 51: | ||
|- | |- | ||
| 1001 | | 1001 | ||
− | + | | | |
− | {| style="" id=" | + | {| style="" id="TABLE96" class="notepad" donotcaption="true" | |
|- | |- | ||
| 1001 | | 1001 | ||
|- | |- | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
| 4 | | 4 | ||
Line 400: | Line 63: | ||
| 9621 | | 9621 | ||
| 8352 | | 8352 | ||
− | |||
− | |||
− | |||
| 6174 | | 6174 | ||
Line 411: | Line 71: | ||
|- | |- | ||
| 1002 | | 1002 | ||
− | + | | | |
− | {| style="" id=" | + | {| style="" id="TABLE97" class="notepad" donotcaption="true" | |
|- | |- | ||
| 1002 | | 1002 | ||
Line 418: | Line 78: | ||
|- | |- | ||
| 3 | | 3 | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
|- | |- | ||
| 2088 | | 2088 | ||
| 8532 | | 8532 | ||
− | |||
− | |||
− | |||
− | |||
| 6174 | | 6174 | ||
Line 441: | Line 91: | ||
| 1003 | | 1003 | ||
| | | | ||
− | {| style="" id=" | + | {| style="" id="TABLE98" class="notepad" donotcaption="true" | |
|- | |- | ||
| 1003 | | 1003 | ||
Line 447: | Line 97: | ||
|- | |- | ||
| 3 | | 3 | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
|- | |- | ||
| 3087 | | 3087 | ||
| 8352 | | 8352 | ||
− | |||
− | |||
− | |||
− | |||
| 6174 | | 6174 | ||
Line 470: | Line 110: | ||
| 1004 | | 1004 | ||
| | | | ||
− | {| style="" id=" | + | {| style="" id="TABLE99" class="notepad" donotcaption="true" | |
|- | |- | ||
| 1004 | | 1004 | ||
Line 476: | Line 116: | ||
|- | |- | ||
| 7 | | 7 | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
|- | |- | ||
Line 499: | Line 133: | ||
| 1005 | | 1005 | ||
| | | | ||
− | {| style="" id=" | + | {| style="" id="TABLE100" class="notepad" donotcaption="true" | |
|- | |- | ||
| 1005 | | 1005 | ||
Line 505: | Line 139: | ||
|- | |- | ||
| 7 | | 7 | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
|- | |- | ||
Line 528: | Line 156: | ||
| 1006 | | 1006 | ||
| | | | ||
− | {| style="" id=" | + | {| style="" id="TABLE101" class="notepad" donotcaption="true" | |
|- | |- | ||
| 1006 | | 1006 | ||
Line 534: | Line 162: | ||
|- | |- | ||
| 7 | | 7 | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
|- | |- | ||
Line 557: | Line 179: | ||
| 1007 | | 1007 | ||
| | | | ||
− | {| style="" id=" | + | {| style="" id="TABLE102" class="notepad" donotcaption="true" | |
|- | |- | ||
| 1007 | | 1007 | ||
Line 563: | Line 185: | ||
|- | |- | ||
| 3 | | 3 | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
|- | |- | ||
| 7083 | | 7083 | ||
| 8352 | | 8352 | ||
− | |||
− | |||
− | |||
− | |||
| 6174 | | 6174 | ||
Line 586: | Line 198: | ||
| 1008 | | 1008 | ||
| | | | ||
− | {| style="" id=" | + | {| style="" id="TABLE103" class="notepad" donotcaption="true" | |
|- | |- | ||
| 1008 | | 1008 | ||
Line 592: | Line 204: | ||
|- | |- | ||
| 3 | | 3 | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
|- | |- | ||
| 8082 | | 8082 | ||
| 8532 | | 8532 | ||
− | |||
− | |||
− | |||
− | |||
| 6174 | | 6174 | ||
Line 615: | Line 217: | ||
| 1009 | | 1009 | ||
| | | | ||
− | {| style="" id=" | + | {| style="" id="TABLE104" class="notepad" donotcaption="true" | |
|- | |- | ||
| 1009 | | 1009 | ||
|- | |- | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
| 4 | | 4 | ||
Line 632: | Line 228: | ||
| 9621 | | 9621 | ||
| 8352 | | 8352 | ||
− | |||
− | |||
− | |||
| 6174 | | 6174 | ||
Line 644: | Line 237: | ||
| 1010 | | 1010 | ||
| | | | ||
− | {| style="" id=" | + | {| style="" id="TABLE105" class="notepad" donotcaption="true" | |
|- | |- | ||
| 1010 | | 1010 | ||
|- | |- | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
| 4 | | 4 | ||
Line 661: | Line 248: | ||
| 9621 | | 9621 | ||
| 8352 | | 8352 | ||
− | |||
− | |||
− | |||
| 6174 | | 6174 | ||
Line 672: | Line 256: | ||
|} | |} | ||
− | + | ||
+ | ==Notes== | ||
+ | |||
+ | In Loops (such as 1..7), | ||
+ | |||
+ | throw("EXIT") | ||
+ | |||
+ | or | ||
+ | |||
+ | throw("BREAK") | ||
+ | |||
+ | will break the FOR loop. | ||
+ | |||
+ | throw("CONTINUE") | ||
+ | |||
+ | will continue the FOR loop call. | ||
+ | |||
+ | In the code above, the throw("EXIT") is used to go break the loop as the loop has reached 6174 potentially. | ||
+ | |||
+ | Also note the elegant way to handle exceptions. For example, tryx or ☝; is a quick way to wrap the previous block or statement with a try{}catch(){} statement. |
Latest revision as of 10:17, 12 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=[]; (1..8)@( 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.⁋.⪡; } ); ☝; return([x,r,rs]) }
x | kc | |||||||||
1001 |
| |||||||||
1002 |
| |||||||||
1003 |
| |||||||||
1004 |
| |||||||||
1005 |
| |||||||||
1006 |
| |||||||||
1007 |
| |||||||||
1008 |
| |||||||||
1009 |
| |||||||||
1010 |
|
Notes
In Loops (such as 1..7),
throw("EXIT")
or
throw("BREAK")
will break the FOR loop.
throw("CONTINUE")
will continue the FOR loop call.
In the code above, the throw("EXIT") is used to go break the loop as the loop has reached 6174 potentially.
Also note the elegant way to handle exceptions. For example, tryx or ☝; is a quick way to wrap the previous block or statement with a try{}catch(){} statement.