var maxi=300;
var imges=[];
imges[0] = new Image(); imges[0].src="rg0.png";
imges[1] = new Image(); imges[1].src="rg1.png";
imges[2] = new Image(); imges[2].src="rg2.png";

function ruler(m) {
	this.n=1;
	this.marks=[];
	this.marks[0]=0;
	this.ndiffs=0;
	this.diff = [];
	this.maxmark=0;
	this.maxi = parseInt(m);
	this.mode= false;
	this.add = function(x) {
		for(var i=0; i<this.n;i++) {
			if(this.diff[x-this.marks[i]]==true) 
				return false;
		}
		for(var i=0; i<this.n;i++) {
			this.diff[x-this.marks[i]]=true;
			this.ndiffs++;
		}
		this.marks[this.n]=x;
		if(x>this.maxmark) this.maxmark = x;
		this.n++;
		return true;
	}
	this.parfait = function() {
		return (this.ndiffs==this.marks[this.n-1]);
	}
	this.teste = function(x) {
                for(var i=0; i<this.n;i++) {
                        if(this.diff[x-this.marks[i]]==true)
                                return false;
                }
                return true;
        }

	this.affiche = function() {
		var s="", s1=this.mode?"0&nbsp; ":"";
		for(var i=1; i<this.n;i++) {
			y = this.marks[i]-this.marks[i-1]
			s1 += (this.mode?this.marks[i]:y+" -")+"&nbsp; ";
			if(s1.length>100) {
				s += s1+"<br />"
				s1=""
			}
		}
		s += s1;
		document.getElementById("ruler").innerHTML="<code>"+s+"</code>"
		this.regle();
		s=""; s1="";
               for(var i=1; i<this.n;i++) {
			s1 += this.marks[i]-this.marks[i-1]+"&nbsp; ";
                        if(s1.length>100) {
                                s += s1+"<br />"
                                s1=""
                        }
                }
                s += s1;
                document.getElementById("differ").innerHTML="<code>"+s+"</code>"

	}

	this.del = function(u) {
		this.n -=u;
		delete(this.diff);
		this.diff=[];
		this.ndiffs=0;
		for(var i=1; i<this.n; i++) {
			for(var j=0; j<i; j++) {
				this.diff[this.marks[i]-this.marks[j]]=true;
				this.ndiffs++;
			}
		}
	}
	this.alea = function(k) {
		var x=Math.floor(k*Math.random());
		do {
			x++;
		}while(this.add(x)==false);
		this.affiche();
		return x;
	}

	this.quest = function() {
		this.mode=(document.frm.rd[0].checked==true);
		s = ''
		for(var x=this.marks[this.n-1]+1; x<=this.maxi; x++) {
		//for(var x=1; x<=this.maxi; x++) {
			if(this.teste(x) == true) {
				y = x-this.marks[this.n-1];
				s += '&nbsp;<span class="spb" onclick="g.add('+x+'); g.quest();">'+(this.mode?x:y)+'</span> '
			}
		}
		g.affiche();
		document.getElementById("choix").innerHTML=s
		document.getElementById("chx").innerHTML=this.n-1;
		var u = (this.n-1)*this.n/2;
		document.getElementById("dist").innerHTML= u;
		document.getElementById("finale").innerHTML= this.marks[this.n-1];
		//document.getElementById("ndiffs").innerHTML= this.ndiffs;
		this.triangle();
	}
	this.next = function(t) {
		var num=0;
                for(var x=this.marks[this.n-1]+1; x<=this.maxi; x++) {
                        if(this.teste(x) == true) {
                                num++
				if(num==t) return x
			} 
		}
		return 0;
        }
	this.triangle = function() {
		var nbchiffres=Math.ceil(Math.log(this.marks[this.n-1])/Math.log(10));
		var places = nbchiffres+1
		//ligne 0
		var s=""
		for(var i=0; i<this.n; i++)  {
			t=this.marks[i]+" "
			for(k=t.length;k<places;k++) {
				t = " "+t
			}
			s += t
		}
		var l = s.length;
		s += "\n"	
		for(i=0; i<l; i++)
			s += "-"
		s += "\n"
		for(var i=1; i<this.n; i++) { // les lignes
			for(k=0; k<(i*places)/2; k++) s += " ";
			for(var j=i; j<this.n; j++) {
				t=this.marks[j]-this.marks[j-i]+" "
                        	for(k=t.length;k<places;k++) {
                                	t = " "+t
                        	}
                        	s += t
			}
			s += "\n"
		}
		document.getElementById("triangle").innerHTML= "<pre>"+s+"</pre>";
	}

	this.seq = function(st) {
		var u = st.replace(/^\s+/,"");
		u = u.replace(/\n+/g," ");
		u = u.replace(/\s+$/,"");
		u = u.replace(/\s+/g," ");
		var a=u.split(/\s/g);
		var current=0;
		s = ""
		for(var i=0; i<a.length; i++) {
			a[i] = parseInt(a[i])
			if(a[i]<=0) a[i]=1;
			s += a[i]+" "
			var x = parseInt(this.next(a[i]));
			
			if(x==0) break;
			this.add(x);
		}
		this.affiche();
		document.getElementById("choix").innerHTML=""
                document.getElementById("chx").innerHTML=this.n-1;
                var u = (this.n-1)*this.n/2;
                document.getElementById("dist").innerHTML= u;
                document.getElementById("finale").innerHTML= this.marks[this.n-1];
		//document.getElementById("triangle").innerHTML= "";
		this.triangle();
		//this.regle();
		return s;
	}
	this.regle = function() {
		var tr = '<img src="rg2.png" width="1" height="20" alt="">'
		var coeffic=0
		if(this.marks[this.n-1] != 0)
			coeffic = 600/this.marks[this.n-1]
		var s = tr
		for(var i=1; i< this.n; i++) {
			var a = i%2, b = Math.round((this.marks[i]-this.marks[i-1])*coeffic);
			s += '<img src="rg'+a+'.png" width="'+b+'" height="20" alt="">'+tr
		}
		document.getElementById("regle").innerHTML=s
	}

	
}


var g;
//alert("AAA")

function init() {
        if(g != null) delete(g);
        maxi = parseInt(document.frm.m.value);
        if(maxi<1 || maxi>10000) maxi=300;
        document.frm.m.value=maxi;
        g = new ruler(maxi);
        g.mode=(document.frm.rd[0].checked==true);
}
function stop() {
        if(itim != null)
                clearInterval(itim)
}
function gol() {
	init();
	g.quest();
}

function efface() {
	g.del(1);
	g.quest();
}

function liste() {
	init();
	document.frm2.s.value = g.seq(document.frm2.s.value);
}

//Math.srandom();


var extab = [
"1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ",
"2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 ",
"3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 ",
"4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 ",
"5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 ",
"1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2",
"1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3",
"1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4",
"1 5 1 5 1 5 1 5 1 5 1 5 1 5 1 5 1 5 1 5 1 5 1 5 1 5 1 5 1 5 1 5",
"1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2",
"1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4",
"1 1 2 1 1 3 1 1 4 1 1 5 1 1 6 1 1 7 1 1 8 1 1 9 1 1 10 1 1 11 1",
"2 3 5 7 11 13 17 19 23 19 31 37 41 43 47 53 5 9 6167 71 73 79 83"
];

var exn=extab.length;

function hasard() {
document.frm2.s.value = extab[Math.floor(exn*Math.random())];
	liste();
}


var optim=[
"1",
"1 3",
"1 4 6",
"1 4 9 11",
"2 7 8 11",
"1 4 10 12 17",
"1 4 10 15 17",
"1 8 11 13 17",
"1 8 12 14 17",
"1 4 10 18 23 25",
"1 7 11 20 23 25",
"1 11 16 19 23 25",
"2 3 10 16 21 25",
"2 7 13 21 22 25",
"1 4 9 15 22 32 34",
"1 5 12 25 27 35 41 44",
"1 6 10 23 26 34 41 53 55",
"2 14 21 29 32 45 49 54 55",
"1 4 13 28 33 47 54 64 70 72",
"1 9 19 24 31 52 56 58 69 72",
"2 8 18 25 39 44 59 68 71 72",
"3 14 16 20 41 48 53 63 71 72",
"2 6 24 29 40 43 55 68 75 76 85",
"2 5 25 37 43 59 70 85 89 98 99 106",
"4 6 20 35 52 59 77 78 86 89 99 122 127",
"4 20 30 57 59 62 76 100 111 123 136 144 145 151",
"1 4 11 26 32 56 68 76 115 117 134 150 163 168 177",
"5 7 17 52 56 67 80 81 100 122 138 159 165 168 191 199",
"2 10 22 53 56 82 83 89 98 130 148 153 167 188 192 205 216",
"1 6 25 32 72 100 108 120 130 153 169 187 190 204 231 233 242 246",
"1 8 11 68 77 94 116 121 156 158 179 194 208 212 228 240 253 259 283",
"2 24 56 77 82 83 95 129 144 179 186 195 255 265 285 293 296 310 329 333",
"1 9 14 43 70 106 122 124 128 159 179 204 223 253 263 270 291 330 341 353 356",
"3 7 17 61 66 91 99 114 159 171 199 200 226 235 246 277 316 329 348 350 366 372",
"9 33 37 38 97 122 129 140 142 152 191 205 208 252 278 286 326 332 353 368 384 403 425"];

var itim;
var nopt;
var meilleure="";
var optlong=0;

function optimale() {
	var nn = parseInt(document.frm3.n.value);
	if (nn!= nopt+1) {
		nopt=nn-1;
		optlong="";
		meilleure="";
	}
	document.frm.m.value=60*(nn);
	if(itim != null) clearInterval(itim);
	itim = setInterval("opt()",200);
}

function opt() {
	var s=""
	//document.frm.m.value=1500;
	for(var i=0; i<nopt; i++) {
		u = 2*nopt-5*i;
		
		u = (u<=2) ? 2 : u;
		s += (Math.floor(u*Math.random())+1)+' '
	}
	document.frm2.s.value=s;
	liste();
	var gn = g.marks[g.n-1]
	var s1="";
	if(optlong==0 || gn<optlong) {
		meilleure = s
		for(i=1; i<g.n; i++) s1 += g.marks[i]+" "
		optlong = gn;
		document.getElementById("meill").innerHTML="<code>0 "+s1+"</code>"
	}
}


