Kaprekar's constant
The number 6174 is known as Kaprekar's constant[1][2][3] 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⁋)#).sort(); kp:=((k⋱)⚯*1 -(k⋰)⚯); _y=k; var r=-1; var rt=-1; var 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⁋)#); //⊫("AFTER",i,_y); } ); } catch(err) { } ⊫([x,r,rt,rs]) return([x,r,_y,rs]) } {| style="" id="TABLE1" class="null wikitable" donotcaption="true" | |- | style="cursor: auto;" | {| style="" id="TABLE1" class="notepad" donotcaption="true" | |- | 1001 |- | style="cursor: auto;" | 4 |- | 6174 |- | 1089 | 9621 | 8352 | 6174 | 6174 | 6174 | 6174 |} |} {| style="" id="TABLE1" class="null wikitable" donotcaption="true" | |- | {| style="" id="TABLE1" class="notepad" donotcaption="true" | |- | 1002 |- | 3 |- | 6174 |- | 2088 | 8532 | 6174 | 6174 | 6174 | 6174 | 6174 |} |} {| style="" id="TABLE1" class="null wikitable" donotcaption="true" | |- | {| style="" id="TABLE1" class="notepad" donotcaption="true" | |- | 1003 |- | 3 |- | 6174 |- | 3087 | 8352 | 6174 | 6174 | 6174 | 6174 | 6174 |} |} {| style="" id="TABLE1" class="null wikitable" donotcaption="true" | |- | {| style="" id="TABLE1" class="notepad" donotcaption="true" | |- | 1004 |- | 7 |- | 6174 |- | 4086 | 8172 | 7443 | 3996 | 6264 | 4176 | 6174 |} |} {| style="" id="TABLE1" class="null wikitable" donotcaption="true" | |- | {| style="" id="TABLE1" class="notepad" donotcaption="true" | |- | 1005 |- | 7 |- | 6174 |- | 5085 | 7992 | 7173 | 6354 | 3087 | 8352 | 6174 |} |} {| style="" id="TABLE1" class="null wikitable" donotcaption="true" | |- | {| style="" id="TABLE1" class="notepad" donotcaption="true" | |- | 1006 |- | 7 |- | 6174 |- | 6084 | 8172 | 7443 | 3996 | 6264 | 4176 | 6174 |} |} {| style="" id="TABLE1" class="null wikitable" donotcaption="true" | |- | {| style="" id="TABLE1" class="notepad" donotcaption="true" | |- | 1007 |- | 3 |- | 6174 |- | 7083 | 8352 | 6174 | 6174 | 6174 | 6174 | 6174 |} |} {| style="" id="TABLE1" class="null wikitable" donotcaption="true" | |- | {| style="" id="TABLE1" class="notepad" donotcaption="true" | |- | 1008 |- | 3 |- | 6174 |- | 8082 | 8532 | 6174 | 6174 | 6174 | 6174 | 6174 |} |} {| style="" id="TABLE1" class="null wikitable" donotcaption="true" | |- | {| style="" id="TABLE1" class="notepad" donotcaption="true" | |- | 1009 |- | 4 |- | 6174 |- | 9081 | 9621 | 8352 | 6174 | 6174 | 6174 | 6174 |} |} {| style="" id="TABLE1" class="null wikitable" donotcaption="true" | |- | {| style="" id="TABLE1" class="notepad" donotcaption="true" | |- | 1010 |- | 4 |- | 6174 |- | 1089 | 9621 | 8352 | 6174 | 6174 | 6174 | 6174 |} |} {| style="" id="TABLE1" class="null withtitle wikitable" donotcaption="true" | |- | x | kc |- | 1001 | style="cursor: col-resize;" | {| style="" id="TABLE1" class="notepad" donotcaption="true" | |- | 1001 |- | 4 |- | 6 | 1 | 7 | 4 |- | 1089 | 9621 | 8352 | 6174 | 6174 | 6174 | 6174 |} |- | 1002 | style="cursor: col-resize;" | {| style="" id="TABLE1" class="notepad" donotcaption="true" | |- | 1002 |- | 3 |- | 6 | 1 | 7 | 4 |- | 2088 | 8532 | 6174 | 6174 | 6174 | 6174 | 6174 |} |- | 1003 | {| style="" id="TABLE1" class="notepad" donotcaption="true" | |- | 1003 |- | 3 |- | 6 | 1 | 7 | 4 |- | 3087 | 8352 | 6174 | 6174 | 6174 | 6174 | 6174 |} |- | 1004 | {| style="" id="TABLE1" class="notepad" donotcaption="true" | |- | 1004 |- | 7 |- | 4 | 1 | 7 | 6 |- | 4086 | 8172 | 7443 | 3996 | 6264 | 4176 | 6174 |} |- | 1005 | {| style="" id="TABLE1" class="notepad" donotcaption="true" | |- | 1005 |- | 7 |- | 8 | 3 | 5 | 2 |- | 5085 | 7992 | 7173 | 6354 | 3087 | 8352 | 6174 |} |- | 1006 | {| style="" id="TABLE1" class="notepad" donotcaption="true" | |- | 1006 |- | 7 |- | 4 | 1 | 7 | 6 |- | 6084 | 8172 | 7443 | 3996 | 6264 | 4176 | 6174 |} |- | 1007 | {| style="" id="TABLE1" class="notepad" donotcaption="true" | |- | 1007 |- | 3 |- | 6 | 1 | 7 | 4 |- | 7083 | 8352 | 6174 | 6174 | 6174 | 6174 | 6174 |} |- | 1008 | {| style="" id="TABLE1" class="notepad" donotcaption="true" | |- | 1008 |- | 3 |- | 6 | 1 | 7 | 4 |- | 8082 | 8532 | 6174 | 6174 | 6174 | 6174 | 6174 |} |- | 1009 | {| style="" id="TABLE1" class="notepad" donotcaption="true" | |- | 1009 |- | 4 |- | 6 | 1 | 7 | 4 |- | 9081 | 9621 | 8352 | 6174 | 6174 | 6174 | 6174 |} |- | 1010 | {| style="" id="TABLE1" class="notepad" donotcaption="true" | |- | 1010 |- | 4 |- | 6 | 1 | 7 | 4 |- | 1089 | 9621 | 8352 | 6174 | 6174 | 6174 | 6174 |} |}