k6174←{⎕ML←1                    ⍝ Kaprekar's operation.
    enco(4/10)∘⊤               ⍝ 4-digit encode.
    decoenco⍣¯1                ⍝   and decode.
    1=⍴∪enco ⍵:⎕SIGNAL 11       ⍝ all digits the same: no go.
    ⍬{                          ⍝ starting with null sequence.
        ⍵=⊃⌽⍺:⍺                 ⍝ repeated items: done.
        v←{⍵[⍒⍵]}enco ⍵         ⍝ digits in descending order.
        (⍺,⍵)(deco v)-decov   ⍝ smaller to larger difference.
    }⍵                          ⍝ :: [#] ∇ # → [#]
}

code_colours

test script

Back to: notes

Back to: Workspaces