Difference between revisions of "Kaprekars Constant"
Jump to navigation
Jump to search
Line 23: | Line 23: | ||
try | try | ||
{ | { | ||
− | (1.. | + | (1..8)@( |
function(i) | function(i) | ||
{ | { | ||
var t=kp(_y) | var t=kp(_y) | ||
+ | |||
if(t==6174 && r==-1) | if(t==6174 && r==-1) | ||
{ | { | ||
Line 37: | Line 38: | ||
} | } | ||
_y=t.⁋.⪪; | _y=t.⁋.⪪; | ||
+ | if(i==8) | ||
+ | { | ||
+ | ⊫(i,"FAILEDFORNUMBER",x) | ||
+ | } | ||
} | } | ||
); | ); | ||
Line 42: | Line 47: | ||
catch(err) | catch(err) | ||
{ | { | ||
+ | // ⊫(err,"ERROR") | ||
} | } | ||
− | ⊫([x,r,rt,rs]) | + | //⊫([x,r,rt,rs]) |
return([x,r,_y,rs]) | return([x,r,_y,rs]) | ||
Line 50: | Line 56: | ||
Answer: | Answer: | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
Line 374: | Line 61: | ||
|- | |- | ||
| x | | x | ||
− | | kc | + | | style="cursor: row-resize;" | kc |
|- | |- | ||
− | | 1001 | + | | style="cursor: col-resize;" | 1001 |
− | | style="cursor: | + | | style="cursor: auto;" | |
{| style="" id="TABLE1" class="notepad" donotcaption="true" | | {| style="" id="TABLE1" class="notepad" donotcaption="true" | | ||
|- | |- | ||
Line 384: | Line 71: | ||
|- | |- | ||
− | | 4 | + | | style="cursor: auto;" | 4 |
|- | |- | ||
− | | | + | | 8 |
− | | | + | | 3 |
− | | | + | | 5 |
− | | | + | | 2 |
|- | |- | ||
| 1089 | | 1089 | ||
− | | 9621 | + | | style="cursor: auto;" | 9621 |
| 8352 | | 8352 | ||
− | |||
− | |||
− | |||
| 6174 | | 6174 | ||
Line 407: | Line 91: | ||
|- | |- | ||
| 1002 | | 1002 | ||
− | | style="cursor: | + | | style="cursor: auto;" | |
{| style="" id="TABLE1" class="notepad" donotcaption="true" | | {| style="" id="TABLE1" class="notepad" donotcaption="true" | | ||
|- | |- | ||
Line 416: | Line 100: | ||
|- | |- | ||
− | | | + | | 8 |
− | | | + | | 5 |
− | | | + | | 3 |
− | | | + | | 2 |
|- | |- | ||
| 2088 | | 2088 | ||
| 8532 | | 8532 | ||
− | |||
− | |||
− | |||
− | |||
| 6174 | | 6174 | ||
Line 445: | Line 125: | ||
|- | |- | ||
− | | | + | | 8 |
− | | | + | | 3 |
− | | | + | | 5 |
− | | | + | | 2 |
|- | |- | ||
| 3087 | | 3087 | ||
| 8352 | | 8352 | ||
− | |||
− | |||
− | |||
− | |||
| 6174 | | 6174 | ||
Line 494: | Line 170: | ||
|- | |- | ||
| 1005 | | 1005 | ||
− | | | + | | style="cursor: auto;" | |
{| style="" id="TABLE1" class="notepad" donotcaption="true" | | {| style="" id="TABLE1" class="notepad" donotcaption="true" | | ||
|- | |- | ||
Line 552: | Line 228: | ||
|- | |- | ||
| 1007 | | 1007 | ||
− | | | + | | style="cursor: auto;" | |
{| style="" id="TABLE1" class="notepad" donotcaption="true" | | {| style="" id="TABLE1" class="notepad" donotcaption="true" | | ||
|- | |- | ||
Line 561: | Line 237: | ||
|- | |- | ||
− | | | + | | 8 |
− | | | + | | 3 |
− | | | + | | 5 |
− | | | + | | 2 |
|- | |- | ||
| 7083 | | 7083 | ||
| 8352 | | 8352 | ||
− | |||
− | |||
− | |||
− | |||
| 6174 | | 6174 | ||
Line 590: | Line 262: | ||
|- | |- | ||
− | | | + | | 8 |
− | | | + | | 5 |
− | | | + | | 3 |
− | | | + | | 2 |
|- | |- | ||
| 8082 | | 8082 | ||
| 8532 | | 8532 | ||
− | |||
− | |||
− | |||
− | |||
| 6174 | | 6174 | ||
Line 619: | Line 287: | ||
|- | |- | ||
− | | | + | | 8 |
− | | | + | | 3 |
− | | | + | | 5 |
− | | | + | | 2 |
|- | |- | ||
Line 628: | Line 296: | ||
| 9621 | | 9621 | ||
| 8352 | | 8352 | ||
− | |||
− | |||
− | |||
| 6174 | | 6174 | ||
Line 639: | Line 304: | ||
|- | |- | ||
| 1010 | | 1010 | ||
− | | | + | | style="cursor: col-resize;" | |
{| style="" id="TABLE1" class="notepad" donotcaption="true" | | {| style="" id="TABLE1" class="notepad" donotcaption="true" | | ||
|- | |- | ||
Line 648: | Line 313: | ||
|- | |- | ||
− | | | + | | 8 |
− | | | + | | 3 |
− | | | + | | 5 |
− | | | + | | 2 |
|- | |- | ||
Line 657: | Line 322: | ||
| 9621 | | 9621 | ||
| 8352 | | 8352 | ||
− | |||
− | |||
− | |||
| 6174 | | 6174 | ||
Revision as of 19:36, 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..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.⁋.⪪; if(i==8) { ⊫(i,"FAILEDFORNUMBER",x) } } ); } catch(err) { // ⊫(err,"ERROR") } //⊫([x,r,rt,rs]) return([x,r,_y,rs]) }
Answer:
x | kc | |||||||||||||
1001 |
| |||||||||||||
1002 |
| |||||||||||||
1003 |
| |||||||||||||
1004 |
| |||||||||||||
1005 |
| |||||||||||||
1006 |
| |||||||||||||
1007 |
| |||||||||||||
1008 |
| |||||||||||||
1009 |
| |||||||||||||
1010 |
|