

function tabl(s) {
	
	this.t=[];
	this.pos=[];
	this.ncrit=0;
	this.ndeg=0
	this.deg=[]
	this.valeurs=[];
	this.mini=0;
        this.maxi=0;
        this.nb=0;

	this.init = function(s) {
		var u = s;
		do {
			v = u;
			u = u.replace(/\;+\s*/g,"\n");
			u = u.replace(/\n+\s+/g,"\n");
			u = u.replace(/\s+\n+/g,"\n");
			u = u.replace(/\s+$/,"");
			u = u.replace(/^\s+/,"")
			u = u.replace(/\s+$/,"")
			//u = u.replace(/([\-\+])\s+$/,"$1")
			u = u.replace(/^\s*#+[^\n]*/,"")
			u = u.replace(/\n\s*\#+[^\n]*/g,"\n")
		} while(u!=v);
		this.t = u.split(/\n+/g);
		for(var i=0; i<this.t.length;i++) {
			var st;
			do {
				st = this.t[i]
				this.t[i]=this.t[i].replace(/^\s+/,"");
				this.t[i]=this.t[i].replace(/\s+/g," ");
				this.t[i]=this.t[i].replace(/\,\s+/g,"\,");
				this.t[i]=this.t[i].replace(/\-\s+/g,"-")
				this.t[i]=this.t[i].replace(/\+\s+/g,"+")
				this.t[i]=this.t[i].replace(/\s*\,*\s*$/,"");
			} while(st != this.t[i]);
			if(this.t[i].search(/\,\s*/g) != -1) {
				this.deg[i] = this.t[i].split(/\,\s*/g);
				with(Math) {
					for(j=0; j<this.deg[i].length; j++) {
						this.deg[i][j]=eval(this.deg[i][j])
					}
				}
			}else {
				this.deg[i] = this.t[i].split(/\s+/g);
			}
		}
		this.ncrit=this.deg[0].length
		this.ndeg = this.deg.length
	}
	this.init(s);

	this.getpolys = function() {
		this.facteurs=[]
		this.poly = "P(x) = "
		for(var i=0; i<this.ncrit; i++) {
			this.facteurs[i]="";
			for(var j=0; j< this.ndeg; j++) {
				this.facteurs[i] += "x"
				this.facteurs[i] += "^"+this.deg[j][i];
				if(j< this.ndeg-1) 
					this.facteurs[i] +=" + "
				
			}
			this.poly += "("+this.facteurs[i]+")"
			if(i<this.ncrit-1) {
				if((i+1)%2==0)this.poly +="\n       * "
				else this.poly += " * "
			}
		}
		return this.poly;
	}

	this.produit = function(t0, t1) {
		var t = new Array();
		for(var x in t0) {
			for(var y in t1) {
				var u = parseFloat(x)+parseFloat(y);
				if(t[u]==null) t[u]=0;
				t[u] += t0[x]*t1[y]
			}
		}
		return t
	}
	//
	this.getpolynome = function() {
		var ta = [], tb = null, tc=null;
		ta[0] = 1;
		for(var i=0; i<this.ncrit; i++) {
			tb = new Array();
			for(x in ta) 
				tb[x] = ta[x];
			delete ta
			tc = new Array();
			for(var j=0; j<this.ndeg; j++) {
				var a = this.deg[j][i]
				if(tc[a] == null) tc[a]=0
				tc[a]+=1
			}
			ta = this.produit(tb, tc);
			delete tc
			delete tb
		}
		this.polynome = ta
		var s=""
		var test = false
		var k=0
		var pmini, pmaxi, xmini, xmaxi;
                for(x in this.polynome) {
			pmini=this.polynome[x];
			xmini = x;
			pmaxi=this.polynome[x];
			xmaxi=x;
			break;
		}
		s2 = "\n------------------------\n"+
			"Somme [nombre de fois], ...\n"
		for(x in this.polynome) {
			k++
			if(this.polynome[x]<pmini){
				mini=this.polynome[x];
				xmini=x;
			}
			if(this.polynome[x]>pmaxi) {
				pmaxi=this.polynome[x];
				xmaxi=x;
			}

			var st = ((this.polynome[x]==1)? "" : this.polynome[x]+"*")
			if(test==true) s += " + "
			test = true
			s += st + "x^"+x +((k%8==0)?"\n      ":"")
			s2 += x+" ["+this.polynome[x]+"], "+((k%8==0)?"\n      ":"")
		}
		this.maxi = pmaxi;
		this.nb=k;
		this.polynomest = "P(x) = "+s;
		this.polynomest += "\nMaximum "+pmaxi+"\n";
		this.polynomest += "nombre de monômes "+k+"\n";
		this.polynomest += s2
		return this.polynomest;
	}

	this.next = function() {
		var ret=1, k=this.ncrit-1;
		while(ret==1) {
			ret=0;
			this.pos[k]++;
			if(this.pos[k]>=this.ndeg) {
			
				ret=1
				this.pos[k]=0;
				k--
				if(k<0) return false;
			}
		}
		return true;
	}

			
	this.calc = function() {
		for(var i=0; i<this.ncrit; i++) this.pos[i]=0;
		do {
			var u=0;
			for(i=0; i<this.ncrit; i++) {
				a = parseFloat(this.deg[this.pos[i]][i]);
				u += a;
			}
			if(this.valeurs[u]==null) {
				this.valeurs[u]=1
			} else {
				this.valeurs[u]++;
			}
		}while(this.next()==true);
	}

	this.affiche = function() {
		var i=0, u="", s="Nombre de résultats différents : "
		var xmin=null, xmax=null, y, nb=0;
		for(x in this.valeurs) {
			x = parseFloat(x);
			i++;
			if(xmin==null) {	xmin=x; xmax=x;}
			if(x<xmin) xmin=x;
			if(x>xmax) xmax=x;
			if(this.valeurs[x]>nb) {
				nb = this.valeurs[x];
				y=x
			}
			u += x+" ["+this.valeurs[x]+"], "
			if(i%8==0) u += "\n";
		}
		this.mini=xmin;
		this.maxi=xmax;
		this.nb=nb;
		s += i+"\n";
		s +="Les sommes vont de "+ xmin +" à "+ xmax +"\n"+
			"La somme la plus souvent calculée est "+y+", elle a été trouvée "+ nb+" fois\n";
		s += u;
		return s;
	}
			
}

function cherche() {
	var ta = new tabl(document.frm.ar.value);
	document.frm2.ar2.value = document.frm.ar.value;
	document.getElementById("res2").innerHTML="<pre>"+ta.getpolys()+"</pre>";
	if(Math.pow(ta.ndeg, ta.ncrit)<=17000) {
		ta.calc();
	document.getElementById("res").innerHTML="Résultats<pre>"+ta.affiche()+"</pre>"
	} else {
	document.frm.ar.value += "\n# Tableau trop grand pour appliquer cette méthode\n"+
		"# allez au paragraphe suivant pour voir le résultat !"
		document.getElementById("res2").innerHTML="<pre>"+
                ta.getpolys() + "\n"+ta.getpolynome()+ "</pre>"
		document.getElementById("res").innerHTML="<br />Désolé, le calcul par cette méthode serait trop long.<br />"+
		"Allez au paragraphe suivant pour voir le résultat, il y est déjà !<br />"
	}
}

function efface() {
	document.frm.ar.value="";
	document.getElementById("res").innerHTML="";
}

function cherch2() {
        var ta = new tabl(document.frm2.ar2.value);
        document.frm.ar.value = document.frm2.ar2.value;
        document.getElementById("res2").innerHTML="<pre>"+
		ta.getpolys() + "\n"+ta.getpolynome()+ "</pre>"
		/* ta.getpolynome()+"</pre>" */
}
function effac2() {
        document.frm2.ar2.value="";
        document.getElementById("res2").innerHTML="";
}

var exemples=[
"10 15 20 15\n"+
"25 25 30 25\n"+
"40 50 40 40\n",

"1 1 2 3\n"+
"2 3 3 4\n"+
"4 5 4 6\n",

"10 10 10 10 10\n"+
"20 20 20 20 20\n"+
"30 30 30 30 30\n"+
"40 40 40 40 40\n",

"10 20 30 40 50\n"+
"20 30 40 50 60\n"+
"30 40 50 60 70\n"+
"40 50 60 70 80\n"+
"50 60 70 80 90\n",

"10 10 10 10 10 10 10\n"+
"20 20 20 20 20 20 20\n"+
"30 30 30 30 30 30 30\n"+
"40 40 40 40 40 40 40\n"+
"50 50 50 50 50 50 50\n",

"10 10 10 10 10\n"+
"20 20 20 20 20\n"+
"30 30 30 30 30\n"+
"40 40 40 40 40\n"+
"50 50 50 50 50\n"+
"60 60 60 60 60\n"+
"70 70 70 70 70\n",

"10 10 30 40 50\n"+
"20 30 50 50 60\n"+
"30 40 60 60 80\n"+
"40 60 70 80 90\n"+
"50 80 90 90 110\n",
"10.8 18.2 13\n"+
"14.8 25.2 16\n"+
"20.8 27.2 19\n",

"39/3,       145/5,              236/8\n"+
"51/3,       40,                 132/3\n"+
"sqrt(1600), floor(50*sqrt(2)),  55\n",

"10 12 15; "+
"16 19 20; "+
"21 24 28",

"11 13 16; "+
"17 20 21; "+
"22 25 29",

"110 112 115\n\n"+
"116 119 120;\n\n"+
"121 124 128;\n",

"10 12 15\n"+
"16 19 20\n"+
"21 24 28\n"+
"30 35 38\n",

"100 120 150\n"+
"160 190 200\n"+
"210 240 280\n"+
"300 350 380",

" 50  60  75\n"+
" 80  95 100\n"+
"105 120 140\n"+
"150 175 190",

"0 0 0 0 0\n"+
"1 1 1 1 1\n"+
"2 2 2 2 2\n"+
"3 3 3 3 3\n",

"0 0 0 0 0\n"+
"1 1 1 1 1\n"+
"2 2 2 2 2\n"+
"3 3 3 3 3\n"+
"4 4 4 4 4\n",

"0 0 0\n"+
"1 1 1\n"+
"2 2 2\n"+
"3 3 3\n"+
"4 4 4\n"+
"5 5 5\n"+
"6 6 6\n"+
"7 7 7\n"

];

var setexo = function(n) {
	document.frm.ar.value=exemples[n];
	document.frm2.ar2.value=exemples[n];
}

var suite = [];

function calculeSuite2(p, q) {
        for(var a=0; a<=p; a++) {
		delete suite[a];
                suite[a]=new Array();
                for(var b=0; b<=q-a; b++) {
                        var s=""
                        for(var i=0; i<=a; i++) {
                                for(var j=0; j<=b; j++) {
                                        s += i
                                        if(j<b) s += " "
                                }
                                if(i<a) s += "\n"
                        }
                        var ta = new tabl(s);
			ta.getpolynome();
                        suite[a][b] = ta.maxi;
                }
        }
}
function lasuite2() {

var u = document.frm3.t.value;
u = u.replace(/\s+/g," ")
u = u.replace(/\-\s+/g,"-")
u = u.replace(/^\s+/,"")
u = u.replace(/\s+$/,"")
var t;
t = u.split(/\s+/g);
var a=parseFloat(t[0]);

calculeSuite2(a,a)
alert(suite)
var s="m lignes n colonnes\n", r=""
for(var i=0; i<=a; i++) {
        for(var j=0; j<=a-i; j++) {
                if(suite[i])s += suite[i][j]+" "
        }
        s +="\n";
}
s += "\nInversés n colonnes m lignes\n"
for(var i=0; i<=a; i++) {
        for(var j=0; j<=a-i; j++) {
                if(suite[j])s += suite[j][i]+" "
        }
        s +="\n";
}

var z=0;
for(var i=0; i<=a; i++) {
        for(var j=i; j>=0; j--) {
		z++;
                if(suite[j]) r += suite[j][i-j]+", "+((z%15==0)?"\n":"");
        }
}



document.getElementById("res3").innerHTML="<pre>"+s+"\n\n"	+r+"</pre>"
}

function cherche2() {
        var ta = new tabl(document.frm2.ar2.value);
        ta.getpolys();
        document.getElementById("res2").innerHTML="Polynôme<pre>"+ta.affiche()+"</pre>"

}


