<?xml version="1.0" encoding="ISO-8859-1"?>
<mdoxml version="1.0">
Here is a machine with four coloured lights. Each light responds to
a rule. <br></br><br></br>
When you type in a number, lights go on if their rule is satisfied.
<br></br>
If the number satisfies more than one rule, then more than one
colour will light up.<br></br><br></br>
Type in some numbers and see which lights you can switch on.<br></br>
<br></br><br></br>

<label>Enter your number:
    <input id="numberText" type="text" value="0" name="number" tabindex="1" maxlength="8" size="8" />
</label>


<br /><br />
<div style="background-color:#444444;width:160px;padding-top:20px;padding-bottom:20px">
    <span id="yellow" style="background-color:#FFCC00;padding:20px;opacity:0.3"> </span>
    <span id="red" style="background-color:#CC3333;padding:20px;opacity:0.3"> </span>
    <span id="blue" style="background-color:#3366FF;padding:20px;opacity:0.3"> </span>
    <span id="green" style="background-color:#00CC33;padding:20px;opacity:0.3"> </span>
</div>
<br />

<script type="text/javascript">
<![CDATA[

	// These functions determine whether to light a colour or not. Return true to light the colour
	function yellowTest(n) {
		return rule[3](n);
	}
	
	function redTest(n) {
		return rule[2](n);
	}
	
	function blueTest(n) {
		return rule[1](n);
	}
	
	function greenTest(n) {
		return rule[0](n);
	}


	// Rule Table - pick the ones you want to use
	var rule = [

		function(n) { return isLinear(n, 2, 1) },
		

		function(n) { return isLinear(n, 5, 0)  },
		
	
		function(n) { return OddTen(n)  },
		

		function(n) { if(n<31) return true; return false },
		
	]
	
	
	// Various number tests
	function isWholeNumber(n) {
		return Math.abs(Math.round(n) - n) < epsilon;
	}
	
	function isMultipleOf(n, a) {
		return isWholeNumber(n/a);
	}
		
	
	function isLinear(n, a, b) {
		return isWholeNumber((n-b)/a);
	}
	
	function isInList(n, list) {
			for(var i=0; i < list.length; i++) {
			if(Math.abs(list[i]-n) < epsilon) return true;
		}
		return false;
	}
	function OddTen(n) {
	m = Math.floor(n/10);
	return isLinear(m, 2, 1);
	}
	
		
	function isPrime(n) {
		if(primes[n]) return true;
		for(var p = 3; p <= Math.sqrt(n); p+=2) {
			if(n%p == 0)
				return false;
		}
		return primes[n]=true;
	}
	
	function isSquare(n) {
		if(n < 0) return false;
		return isWholeNumber(Math.sqrt(n));
	}
	
	function isTriangular(n) {
		return isSquare(8*n+1);
	}
	
	// Pick a random whole number in [a,b]
	function randomRange(a,b) {
		return Math.floor(a + (b-a)*Math.random());
	}
	
	// return the digits of the number as an Array
	function digits(n) {
		ar = [];
		if(!isWholeNumber(n)) return ar;
		return pushDigits(ar,n);
	}
	function pushDigits(ar, n) {
		m = Math.round(n/10);
		d = n - 10*m;
		ar.push(d);
		if(m > 0)
			return pushDigits(ar, m);
		return ar;
	}
	
	// return the sumDigits
	function sumDigits(n) {
		var ar = digits(n);
		var sum = 0;
		for(var i=0; i < ar.length; i++) {
			sum += digits[i];
		}
		return sum;
	}
	
	// keep summing digits till we have a digit
	function digitSum(n) {
		if(!isWholeNumber(n)) return -1;
		for(s = n; s >= 10; s = sumDigits(n)) {}
		return s;
	}
	
	
	var epsilon = 1e-13;
	var primes = {2:true};


	// Apply the tests to the number
	function testIt() {
		var number = $("#numberText").attr("value");
		var n = parseInt(number);
		
		if(!isNaN(n)) {
			lightup("#yellow", yellowTest(n));
			lightup("#red", redTest(n));
			lightup("#green", greenTest(n));
			lightup("#blue", blueTest(n));			
		}
		else {
			flicker("#yellow");
			flicker("#red");
			flicker("#green");
			flicker("#blue");
		}
	}
	
	function lightup(id, on) {
		$(id).stop();
		$(id).animate({opacity: (on ? 1 : 0)}, 250)
	}
	
	function flicker(id) {
		$(id).animate({opacity: Math.random()*0.5}, 50*(1+Math.random()), "linear", function() {flicker(id)});
	}
	
	function init() {
		//$("#numberText").change(testIt);
		$("#numberText").keyup(testIt);
	}

	init();
	restart();

]]>
</script>

 <br></br><br></br><br></br>
What is the smallest number which lights them all up?<br></br><br></br>


</mdoxml>
