Look-and-say sequence: Difference between revisions

Content deleted Content added
Not a robot (talk | contribs)
Add Draco
Line 2,910: Line 2,910:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Custom Functions:
Custom Functions:
<lang Mathematica>RunLengthEncode[x_List]:=(Through[{First,Length}[#]]&)/@Split[x]
<lang Mathematica> LookAndSay[n_Integer?Positive]:=Map[Reverse,Tally/@Split@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>NestList[LookAndSay, 1, 12] // Column</lang>
gives back:

returns


<pre style='height:15em;overflow:scroll'>1
<pre style='height:15em;overflow:scroll'>1
Line 2,934: Line 2,934:
1321132132111213122112311311222113111221131221
1321132132111213122112311311222113111221131221


Entering 7:
7

<lang Mathematica>NestList[LookAndSay, 7, 12] // Column</lang>

generates:

<pre style='height:15em;overflow:scroll'>7
17
17
1117
1117