Brock MacDonald Brock MacDonald - 4 months ago
974 0

Page for automatically calculating Thue-Morse sequences. The page contains 4 files:


  1. Main Page: with a select drop-down)
  2. Get Sequence: script that generates the Thue-Morse sequence and outputs the result
  3. JavaScript: script that passes the value of the drop-down to the Get Sequence script
  4. Styling: makes everything look decent and avoid in-line styling by creating classes for the various types of table cells

PHP

MacDonald | Main Page | Thue-Morse Sequencer

<?php
	echo "
		<head>
			<link href='https://fonts.googleapis.com/css?family=Arimo|Source+Sans+Pro' rel='stylesheet' type='text/css'>
	
		<style>";
			include "style.css";
	echo "
		</style>";
	
	include "scripts.js";	
	
	echo "</head>";
?>
	<center>
		<h1>Thue-Morse Sequence</h1>
		<a href="https://en.wikipedia.org/wiki/Thue%E2%80%93Morse_sequence#Direct_definition" title="via Wikipedia" target="_blank">More Information</a><br>
		<br>
		Select a continuous sequence of whole numbers to consider...<br>
		<select id="integers" name="integers" onchange="getResults(this.value);" autofocus>
			<option value="NULL" selected disabled>(Select One)</option>
			<option value="4">4</option>
			<option value="8">8</option>
			<option value="16">16</option>
			<option value="32">32</option>
			<option value="64">64</option>
			<option value="128">128</option>
			<option value="256">256</option>
		</select>
		<br>
		<br>
		<br>
		<span id="bigTable"></span>
	</center>
PHP

MacDonald | Get Sequence | Thue-Morse Sequencer

<?php
	//define array paramters
	$count = intval($_GET['count']);
	
	$initial_array_size = $count;
	$initial_array = array();
	$selected_array = array();
	
	$i_init = 0;
	$i = $i_init;

	//build initial array
	while ($i < $initial_array_size) {
	array_push($initial_array, $i);
	$i++;
	}
	
	$odd_array = array();
	$even_array = array();
	
	foreach ($initial_array as $vals) {
		if(array_sum(str_split(decbin($vals))) % 2 != 0) {
			array_push($odd_array,$vals);
		} else {
			array_push($even_array,$vals);
		}
	}
?>
<strong>Consider all whole numbers between <u><?php echo $i_init; ?></u> and <u><?php echo ($i_init + $initial_array_size - 1); ?></u>.</strong><br><br><br>
<table>
<col width="22%">
<col width="11%">
<col width="11%">
<col width="11%">
<col width="11%">
<col width="11%">
<col width="11%">
<col width="11%">
	<tr>
		<td class="c1r1">Whole number groups</td>
		<td class="t_head">&Sigma;(1st Powers)</td>
		<td class="t_head">&Sigma;(2nd Powers)</td>
		<td class="t_head">&Sigma;(3rd Powers)</td>
		<td class="t_head">&Sigma;(4th Powers)</td>
		<td class="t_head">&Sigma;(5th Powers)</td>
		<td class="t_head">&Sigma;(6th Powers)</td>
		<td class="t_head">&Sigma;(7th Powers)</td>
	</tr>
	<tr>
		<td class="t_row_head">
			<?php
				$co = 1;
				foreach ($odd_array as $odd) {
					echo $odd;
					if ($co > $count / 2 - 1) { break; } else {
						echo " + ";
						$co++;
					}
				}
			?>
		</td>
		<?php 
			$o = 1;
			while ($o <= (log($count)/log(2) - 1)) {
				echo "
					<td class='t_row_data'>";
						$odd_a = $odd . $o;
						$odd_a = array();
						foreach ($odd_array as $odd) {
							$odd = pow($odd,$o);
							array_push($odd_a, $odd);
						}
						echo number_format(array_sum($odd_a)) . "</td>
					";
				$o++;
			}
		?>
	</tr>
	<tr>
		<td class="t_row_head">
			<?php
				$ce = 1;
				foreach ($even_array as $even) {
					echo $even;
					if ($ce > $count / 2 - 1) { break; } else {
						echo " + ";
						$ce++;
					}
				}
			?>
		</td>
			<?php 
				$e = 1;
				while ($e <= (log($count)/log(2) - 1)) {
					echo "
						<td class='t_row_data'>";
							$even_a = $even . $e;
							$even_a	= array();
							foreach ($even_array as $even) {
								$even = pow($even,$e);
								array_push($even_a, $even);
							}
							echo number_format(array_sum($even_a)) . "</td>
						";
					$e++;
				}
			?>
	</tr>
</table>
</center>
Javascript

MacDonald | JavaScript | Thue-Morse Sequencer

<script>
	function getResults(val) {
			if (window.XMLHttpRequest) {
				// code for IE7+, Firefox, Chrome, Opera, Safari
				xmlhttp = new XMLHttpRequest();
			} else {
				// code for IE6, IE5
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			xmlhttp.onreadystatechange = function() {
				if (this.readyState == 4 && this.status == 200) {
					
					document.getElementById("bigTable").innerHTML = this.responseText;
				}
			};
			xmlhttp.open("GET","get_results.php?count="+val,true);
			xmlhttp.send();
	}
</script>
CSS

MacDonald | Styling | Thue-Morse Sequencer

body {
	font-family: 'Arimo', sans-serif; 
}

table {
	width:100%;
	border-collapse:collapse;
}

td.c1r1 {
	padding:5px;
	font-weight:bold;
	vertical-align:bottom;
}

td.t_head {
	border:1px solid black;
	padding:5px;
	vertical-align:bottom;
}

td.t_row_head {
	font-size:75%;
	border:1px solid black;
	padding:5px;
}

td.t_row_data {
	border:1px solid black;
	vertical-align:top;
	text-align:left;
	padding:5px;
}