The   Look and say sequence   is a recursively defined sequence of numbers studied most notably by   John Conway.



The   look-and-say sequence   is also known as the   Morris Number Sequence,   after cryptographer Robert Morris,   and the puzzle   What is the next number in the sequence 1,   11,   21,   1211,   111221?   is sometimes referred to as the Cuckoo's Egg,   from a description of Morris in Clifford Stoll's book   The Cuckoo's Egg.


Sequence Definition

 Take a decimal number
 Look at the number, visually grouping consecutive runs of the same digit.
 Say the number, from left to right, group by group; as how many of that digit there are - followed by the digit grouped.
 This becomes the next number of the sequence.

An example:

 Starting with the number 1,   you have one 1 which produces 11
 Starting with 11,   you have two 1's.   I.E.:   21
 Starting with 21,   you have one 2, then one 1.   I.E.:   (12)(11) which becomes 1211
 Starting with 1211,   you have one 1, one 2, then two 1's.   I.E.:   (11)(12)(21) which becomes 111221


Task

Write a program to generate successive members of the look-and-say sequence.



Related tasks

   Fours is the number of letters in the ...
   Number names
   Self-describing numbers
   Self-referential sequence
   Spelling of ordinal numbers


See also

   Look-and-Say Numbers (feat John Conway), A Numberphile Video.
   This task is related to, and an application of, the Run-length encoding task.
   Sequence A005150 on The On-Line Encyclopedia of Integer Sequences.


 
Public Sub LookAndSay(Optional Niter As Integer = 10)
'generate "Niter" members of the look-and-say sequence
'(argument is optional; default is 10)
 
Dim s As String            'look-and-say number
Dim news As String         'next number in sequence
Dim curdigit As String     'current digit in s
Dim newdigit As String     'next digit in s
Dim curlength As Integer   'length of current run
Dim p As Integer           'position in s
Dim L As Integer           'length of s
 
On Error GoTo Oops          'to catch overflow, i.e. number too long
 
'start with "1"
s = "1"
For i = 1 To Niter
  'initialise
  L = Len(s)
  p = 1
  curdigit = Left$(s, 1)
  curlength = 1
  news = ""
  For p = 2 To L
    'check next digit in s
    newdigit = Mid$(s, p, 1)
    If curdigit = newdigit Then 'extend current run
      curlength = curlength + 1
    Else ' "output" run and start new run
      news = news & CStr(curlength) & curdigit
      curdigit = newdigit
      curlength = 1
    End If
  Next p
  ' "output" last run
  news = news & CStr(curlength) & curdigit
  Debug.Print news
  s = news
Next i
Exit Sub
 
Oops:
  Debug.Print
  If Err.Number = 6 Then 'overflow
    Debug.Print "Oops - number too long!"
  Else
    Debug.Print "Error: "; Err.Number, Err.Description
  End If
End Sub
 