Look-and-say sequence: Difference between revisions

(Add Draco)
Line 2,910:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Custom Functions:
<lang Mathematica>RunLengthEncode LookAndSay[x_Listn_Integer?Positive]:=(ThroughMap[{FirstReverse,Length}[#]]&)Tally/@Split[x@IntegerDigits@n,2]//Flatten//FromDigits</lang>
LookAndSay[n_,d_:1]:=NestList[Flatten[Reverse/@RunLengthEncode[#]]&,{d},n-1]</lang>
 
This function, which works for arbitrary positive integers, starts with `n` and generates the next member of the sequence.
If second argument is omitted the sequence is started with 1. Second argument is supposed to be a digits from 0 to 9. If however a larger number is supplied it will be seen as 1 number, not multiple digits. However if one wants to start with a 2 or more digit number, one could reverse the sequence to go back to a single digit start. First example will create the first 13 numbers of the sequence starting with 1, the next example starts with 7:
 
The first example returns the first 13 numbers of the sequence starting with 1. Entering
<lang Mathematica>FromDigits /@ LookAndSay[13] // Column
FromDigits /@ LookAndSay[13, 7] // Column</lang>
 
<lang Mathematica>FromDigitsNestList[LookAndSay, /@1, LookAndSay[1312] // Column</lang>
gives back:
 
returns
 
<pre style='height:15em;overflow:scroll'>1
Line 2,934:
1321132132111213122112311311222113111221131221
 
Entering 7:
7
 
<lang Mathematica>NestList[LookAndSay, 7, 12] // Column</lang>
 
generates:
 
<pre style='height:15em;overflow:scroll'>7
17
1117