/*    	digitpos.js   
	Suites digitales autodescriptives de type I
      	Jean-Paul Davalan   (C) 2009 
	http://jeux-et-mathematiques.davalan.org/mots/suites/digitposition/index.html
	

	Référence pour l'origine de ces suites : Éric Angelini 
	http://www.cetteadressecomportecinquantesignes.com/DigitPosition.htm
*/
/* ---------------------- MAIN FUNCTION --------------------------- */
function digitpos(x, lim) {
var car =""+x, deb=0, bits = [], u = [], s = [], p = [];
u[0]=0;s[0]="0";p[0]=0;
if(x==0) {
	u[1]=3;s[1]="3";p[1]=1;bits[3]="0";deb=1;
}
function suivant(n) {
	var l = s[n].length;
	p[n+1] = p[n] + l;
	for(var i=0;i<=l; i++) 
		if(bits[p[n]+i]==null) 
			bits[p[n]+i]="*";
	var k=u[n];
	do { 
		k++; 
	} while(! compatible(k, p[n+1]));
	bits[k]=car; u[n+1]=k;s[n+1] = ""+k;
	return k;
}
function compatible(k, p) {
	var r = ""+k;
	if(bits[p+r.length]=="0") return false
	for(var i=0; i<r.length; i++)  {
		if( (p+i == k && r.charAt(i)!=car ) ||
		  (bits[p+i]==car && r.charAt(i)!= car) ||
		  (bits[p+i]!=car && p+i != k && r.charAt(i)== car) ) 
			return false;
	}
	return true
}
for(var i=deb; i<lim; i++)  suivant(i)
var st = "<u>Suite u<sub>"+x+"</sub>(n) pour c="+x+"  jusqu'à n="+lim+"</u><br>"
for(var i=1; i<=lim;i++)
	st += u[i]+((i<lim)?", ":"")
return st
}
/* ------------------------------------------------------------ */
function cherche() {
	var c = parseInt(document.frm.c.value)%10
	c = (c<0)?-c : c
	document.frm.c.value = c
	var lim = parseInt(document.frm.lim.value)
	lim = (lim<10) ? 10 : lim
	document.frm.lim.value = lim
	document.getElementById("idSols").innerHTML= digitpos(c, lim)
}
function suivt(k) {
	document.frm.c.value = (k+parseInt(document.frm.c.value))%10
	cherche()
}
function efface() {
	document.getElementById("idSols").innerHTML=""
	document.frm.c.value=""
	document.frm.lim.value=""
}

