Appended Numbers Game

APL (66)

{↑¯5↑{(⍕⍵),': ',⍺}/∆,⍪⍳⍴∆←⍺{(1<⍴⍵)∧⍺>0:∆,(⍺-1)∇⊃∆←,/⍕¨2+/⍎¨⍵⋄⍬}⍕⍵}

The left argument is the maximum iteration count and the right argument is the start number.

Explanation:

  • ∆←⍺{...}⍕⍵: pass the left argument as a number and the right argument as a string to the function that calculates the list of numbers, and store it in :
    • (1<⍴⍵)∧⍺>0:: if the amount of digits is more than 1 and the amount of iterations left is more than 0:
      • ⍎¨⍵: evaluate each digit
      • 2+/: sum each pair
      • ⍕¨: format each number as a string
      • ∆←,/: concatenate the strings and store in
      • ∆,(⍺-1)∇⊃∆: return , followed by the result of applying this function to with one less iteration allowed
    • ⋄⍬: if not, return the empty list
  • ∆,⍪⍳⍴∆: pair each element of with its index in
  • {...}/: for each pair:
    • (⍕⍵),': ',⍺: return a string with the index, followed by :, followed by the number
  • ↑¯5↑: turn the list of strings into a matrix so they display on separate lines, and take the last 5 items

Test:

      5{↑¯5↑{(⍕⍵),': ',⍺}/∆,⍪⍳⍴∆←⍺{(1<⍴⍵)∧⍺>0:∆,(⍺-1)∇⊃∆←,/⍕¨2+/⍎¨⍵⋄⍬}⍕⍵}3541
1: 895 
2: 1714
3: 885 
4: 1613
5: 774 
      50{↑¯5↑{(⍕⍵),': ',⍺}/∆,⍪⍳⍴∆←⍺{(1<⍴⍵)∧⍺>0:∆,(⍺-1)∇⊃∆←,/⍕¨2+/⍎¨⍵⋄⍬}⍕⍵}3541
6: 1411
7: 552 
8: 107 
9: 17  
10: 8  

Mathematica, 172 characters

This is way too long, thanks to Mathematica's function names and ugly string handling (the actual "game" is only 76 of those characters), but here it is anyway:

""<>ToString/@(f=Flatten)@Take[Thread@{r=Range@Length[s=Rest@Cases[NestList[FromDigits[f@(d=IntegerDigits)[Tr/@Partition[d@#,2,1]]]&,n,m],i_/;i>0]],": "&/@r,s,"\n"&/@r},-5]

It expects the input number in variable n and the maximum number of iterations in m.

With less golf:

"" <> ToString /@
  (f = Flatten)@
   Take[
    Thread@{
      r = Range@Length[
         s = Rest@Cases[
            NestList[                 
             FromDigits[
               f@(d = IntegerDigits)[Tr /@ Partition[d@#, 2, 1]]] &,
             n,
             m
             ],
            i_ /; i > 0
            ]
         ],
      ": " & /@ r,
      s,
      "\n" & /@ r
      },
    -5
    ]

Ruby, 106 characters

f=->n,m{s=0
$*<<"#{s}: #{n=n.to_s.gsub(/.\B/){eval$&+?++$'[0]}.chop}"until n.to_i<10||m<s+=1
puts$*.pop 5}

I'm not 100% clear on the input rules, but if I can take n as a string I can save 5 characters, and if I can use predefined variables and write a program instead of a function, I can save another 9.

Creates a function f which can be called as follows:

f[3541, 6]

2: 1714
3: 885
4: 1613
5: 774
6: 1411

f[372, 50]

1: 109
2: 19
3: 10
4: 1

f[9999, 10]

6: 99999999999
7: 18181818181818181818
8: 9999999999999999999
9: 181818181818181818181818181818181818
10: 99999999999999999999999999999999999

Tags:

Code Golf