2
\$\begingroup\$

There is a sequence of numbers called the "Look-and-Say Numbers".

Here is an example of the first 5 terms of the sequence, starting at 1 (with spaces added between the digits):

     1
    1 1
    2 1
  1 2 1 1
1 1 1 2 2 1

The pattern is simple, once you see it.

The sequence starts with 1. There is one 1, so the next number is 11 (as in one of the number 1).

In this number, there are 2 1s, so the next number is 21.

In this number there is 1 2, and 1 1, so the next number is 1211.

In this number there is 1 1, followed by 1 2, followed 2 1s - so the number is 111221.

The Task

You must write a program that generates the first n terms of this sequence, where n is given by stdin. The sequence will start at 1.

This is a code-golf task, so shortest code wins.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ @Calvin'sHobbies The linked question has an additional restriction, but that restriction is quite trivial in practice. Therefore I am voting to close as a duplicate. \$\endgroup\$ Aug 12 '14 at 11:59
0
\$\begingroup\$

Mathematica, 68 bytes

There's no STDIN in Mathematica, so here is a function instead

s=FromDigits/@NestList[Flatten[Reverse@@@Tally/@Split[#]]&,{1},#-1]&

Usage:

s[5]

> {1, 11, 21, 1211, 111221}
\$\endgroup\$
3
  • \$\begingroup\$ Isn't Input[] STDIN? \$\endgroup\$
    – DavidC
    Aug 12 '14 at 13:01
  • \$\begingroup\$ @DavidCarraher Well, it's kinda STDIN but parses and interprets the input right away, right? \$\endgroup\$ Aug 12 '14 at 13:02
  • \$\begingroup\$ Good point. And InputString[] avoids parsing but interprets all input as strings. \$\endgroup\$
    – DavidC
    Aug 12 '14 at 13:22

Not the answer you're looking for? Browse other questions tagged or ask your own question.