Thue-Morse: Difference between revisions

Content added Content deleted
m (added link)
(added =={{header|Pascal}}==)
Line 147: Line 147:
| ; Repeat loop</lang>
| ; Repeat loop</lang>
The standard DOS-based interpreter will display an error message about word too long after 7 lines are output; this is because the 8th line does not fit in 80 columns.
The standard DOS-based interpreter will display an error message about word too long after 7 lines are output; this is because the 8th line does not fit in 80 columns.
=={{header|Pascal}}==
{{works with|Free Pascal}}
<lang pascal>
Program ThueMorse;


procedure NextThueMorse(var s: AnsiString);
//double length and append by flipping every original 0 -> 1;1 -> 0
//flipping x between A;B - > x_next = A+B-x
var
i : NativeInt;
pOrg,
pRpl : pChar;
Begin
IF s = '' then
Begin
s := '0';
EXIT;
end;
i := length(s);
setlength(s,i+i);
pOrg := @s[1];
pRpl := @s[i+1];
repeat
dec(i);
pRpl[i] := chr(Ord('1')+Ord('0')-Ord(pOrg[i]));
until i<=0;
end;

procedure NextThueMorseLSystem(var s: AnsiString);
//double length and replace every 0 -> 01 and 1 -> 10
//doing it backwards, so no extra copy is needed.
const
cReplace : array['0'..'1'] of String[2] = ('01','10');
var
i : NativeInt;
pS,pR : pChar;
Begin
IF s = '' then
Begin
s := '0';
EXIT;
end;
i := length(s);
setlength(s,i+i);
pS := @s[i+i];
dec(ps);
repeat
pR := @cReplace[s[i]];
pS[ 1]:= pR[2];
pS[ 0]:= pR[1];
dec(ps,2);
dec(i);
until i<=0;
end;

var
s: AnsiString;
i : integer;
Begin
For i := 0 to 30 do
Begin
NextThueMorse(s);
IF i < 6 then
writeln(s);
end;
writeln(length(s));
end.</lang>
{{Output}}<pre>//NextThueMorse
0
01
0110
01101001
0110100110010110
01101001100101101001011001101001
1073741824 ( 1 GB)
real 0m1.162s user 0m0.689s sys 0m0.472s //memory allocation
//NextThueMorseLSystem
Same Output
real 0m1.981s user 0m1.492s sys 0m0.486s
</pre>
=={{header|Perl 6}}==
=={{header|Perl 6}}==
{{Works with|rakudo|2015-12-22}}
{{Works with|rakudo|2015-12-22}}