Thue-Morse: Difference between revisions
Content added Content deleted
(added =={{header|Pascal}}==) |
m (→{{header|Pascal}}: use a function with length set once. 50% faster than before. ^ v as alias for 0 1) |
||
Line 149:
=={{header|Pascal}}==
{{works with|Free Pascal}}
Like the C++ Version [[http://rosettacode.org/wiki/Thue-Morse#C.2B.2B]] the lenght of the sequence is given in advance.
<lang pascal>Program ThueMorse;
function fThueMorse(maxLen: NativeInt):AnsiString;
//Flipping between two values:x oszillating A,B,A,B -> x_next = A+B-x
//Beware A+B < High(Char), the compiler will complain ...
const▼
cVal0 = '^';cVal1 = 'v';// cVal0 = '0';cVal1 = '1';
▲//double length and append by flipping every original 0 -> 1;1 -> 0
var
i : NativeInt;▼
pOrg,
pRpl : pChar;
i,k,ml : NativeUInt;//MaxLen: NativeInt
Begin
Begin
EXIT;
end;
//setlength only one time
i := length(s);▼
setlength(
pOrg := @s[1];▼
repeat▼
dec(i);▼
pRpl[i] := chr(Ord('1')+Ord('0')-Ord(pOrg[i]));▼
until i<=0;▼
pOrg[0] := cVal0;
▲const
▲ IF s = '' then
EXIT;
repeat
until i
until k+k> ml;
// the rest
▲ repeat
inc(i)
end;
var
i : integer;
Begin
For i := 0 to
writeln(i:3,' ',fThueMorse(i));
fThueMorse(1 shl 30);
▲ IF i < 6 then
▲ writeln(s);
end.</lang>
{{Output}}<pre>Compile with /usr/lib/fpc/3.0.1/ppc386 "ThueMorse.pas" -al -XX -Xs -O4 -MDelphi
without -O4 -> 2 secs
0
1 ^
2 ^v
3 ^vv
4 ^vv^
5 ^vv^v
6 ^vv^v^
real 0m1.162s user 0m0.689s sys 0m0.472s //memory allocation ▼
7 ^vv^v^^
8 ^vv^v^^v
not written: 1 shl 30 == 1GB
=={{header|Perl 6}}==
{{Works with|rakudo|2015-12-22}}
|