Appended Numbers Game
Write a function/program that takes 2 integer parameters integer parameters or integer variables, a start number, and a max iterations count. The code should perform the following game example to construct a new number, and repeat until the number is a single digit left. eg.
3 7 2 = (3 + 7) & (7 + 2) = 10 9
1 0 9 = (1 + 0) & (0 + 9) = 1 9
1 9 = (1 + 9) = 10
1 0 = (1 + 0) = 1
Basically, taking each individual digit and adding it to its neighbour, then appending the result of the next addition as well.
Max iteration count is to safeguard infinite loops, and when the max is hit, code should dump the last 5 number steps. The same output should occur when finishing by reaching a single digit. If less than 5 steps occurred, only output the valid numbers.
Output should appear like (Step: Number
) including the last 5 steps of the finished or terminated steps:
func(3541, 50)
would produce this exact output format:
6: 1411
7: 552
8: 107
9: 17
10: 8
func(3541, 5)
would produce:
1: 895
2: 1714
3: 885
4: 1613
5: 774
The entire calculation being:
1: 895
2: 1714
3: 885
4: 1613
5: 774
6: 1411
7: 552
8: 107
9: 17
10: 8
If there are less than 5 steps, just print those steps taken.
Only use built-in libs, parameters can be from anywhere (whatever's easiest for your language of choice). No limit on maximum integer size, and if there are overflows, let it crash.
Given this isn't too difficult from a puzzle point of view, I will give until Sunday 25th, 8PM (UTC+8) for submissions to be considered for the accepted answer, at which point the shortest of any language will be the winner.
EDIT:
Congratulations to Howard, winning with a 48 GolfScript answer.
Special mention to 2nd place marinus with a 66 APL answer.
My personal favourite (being biased towards JavaScript) was core1024's answer.
func(3541, 5)
supposed to print 5 steps or 10? \$\endgroup\$