Jump to content

Thue-Morse: Difference between revisions

No edit summary
Line 1,176:
inverte chain
endfor</lang>
 
=={{header|PHP}}==
 
Fast sequence generation - This implements the algorithm that find the highest-order bit in
the binary representation of n that is different from the same bit in the representation of n − 1
(see https://en.wikipedia.org/wiki/Thue%E2%80%93Morse_sequence)
 
<lang PHP><?php
 
function thueMorseSequence($length) {
$sequence = '';
for ($digit = $n = 0 ; $n < $length ; $n++) {
$x = $n ^ ($n - 1);
if (($x ^ ($x >> 1)) & 0x55555555) {
$digit = 1 - $digit;
}
$sequence .= $digit;
}
return $sequence;
}
 
for ($n = 10 ; $n <= 100 ; $n += 10) {
echo sprintf('%3d', $n), ' : ', thueMorseSequence($n), PHP_EOL;
}</lang>
 
{{out}}
<pre> 10 : 0110100110
20 : 01101001100101101001
30 : 011010011001011010010110011010
40 : 0110100110010110100101100110100110010110
50 : 01101001100101101001011001101001100101100110100101
60 : 011010011001011010010110011010011001011001101001011010011001
70 : 0110100110010110100101100110100110010110011010010110100110010110100101
80 : 01101001100101101001011001101001100101100110100101101001100101101001011001101001
90 : 011010011001011010010110011010011001011001101001011010011001011010010110011010010110100110
100 : 0110100110010110100101100110100110010110011010010110100110010110100101100110100101101001100101100110</pre>
 
=={{header|PicoLisp}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.