
function myeval(s) {
  var a = eval(s), u = Math.round(a), e = Math.abs(u-a);
  if(a!=0  && e/a < 0.00000001) a = u;
  return a;
}

function devine() {
    this.Ensble = null;
    this.S = new Array();
    this.Res = null;
    this.H = new Array();
    this.Hmax=0; this.Hmin = 0;
    function lire() {
      var s, s0 = document.frm.t.value, u;
      var table = s0.split(/[\=\>\:\;]+/g)
      s = table[0];
      if(table.length>1) {
         this.Res = parseInt(table[1]);
      }
      do {
        u=s;
        s = s.replace(/^\s+/,"");
        s = s.replace(/\s+$/,"");
        s = s.replace(/\s+/g," ");
        s = s.replace(/\+\s+/g," ");
        s = s.replace(/\-\s+/g,"-");
      }while(s != u);
     this.Ensble =  s.split(/\s/g);
    }
    this.lire = lire;

    function f(X) {
       var n = X.length;
       if(n==1) {
          this.S.push(X[0]);
       } else {
         var t = new Array();
         for(var j=0; j<n-1; j++) {
           for(var k=j+1; k<n; k++) {
               var z=1;
               for(var l=0; l<n; l++) {
                 if(l!=j && l!= k) {
                    t[z] = X[l]
                    z++
                 }
               }
                t[0] = "("+X[j]+"+"+X[k]+")"; this.f(t);
                t[0] = "("+X[j]+"*"+X[k]+")"; this.f(t);
                t[0] = "("+X[j]+"-"+X[k]+")"; this.f(t);
                t[0] = "("+X[j]+"/"+X[k]+")"; this.f(t);
                t[0] = "("+X[k]+"-"+X[j]+")"; this.f(t);
                t[0] = "("+X[k]+"/"+X[j]+")"; this.f(t);
           }
         }
       }
    }
    this.f = f;

    function sols() {
       document.frm.ecp.value = " ";
       this.lire();
       
       this.f(this.Ensble)
       var nbsols = 0;
       var s = "";
       var listesols = "";
       for(var i=0; i < this.S.length; i++) {
          var y = myeval(this.S[i]);
          if(y%1 ==0) {
             this.H[y] = (this.H[y]==null)? 1 : this.H[y]+1;
             if(y<this.Hmin) this.Hmin=y;
             if(y>this.Hmax) this.Hmax=y;
             if(this.Res != null && y==this.Res) {
                s = s+"SOLUTION :\n"
                nbsols++;
                listesols += this.S[i]+" = "+y+"\n"
             }
             s = s+this.S[i]+" = "+y+"\n"
          } else {
             s = s+this.S[i]+" = "+y+" invalide\n"
          }
       }
       var s1 = "\nNombre de solutions : "+nbsols+"\n";
       if(nbsols>0) s1 += "Liste des solutions :\n"+listesols+"-------------------\n";
       var s2 = "uniques : ";
       for(var j=this.Hmin; j<= this.Hmax; j++) {
         if(this.H[j]!=null && this.H[j]==1) s2 += j+" ";
       }
       s2 += "\n-------------------\n";
       s = s1 + s2 + s;
       document.frm.ecp.value=s;
    }
    this.sols = sols;

}

function cherche() {
  var D = new devine();
  D.sols();
}

function efface() {
document.frm.t.value = "";
document.frm.ecp.value = "";
}
      
function sel() {
document.frm.ecp.select();
}

var Exemples = new Array(
"1 5 6 7 ; 21",
"2 3 5 11 ; 3",
"1 7 11 12 ; 33",
"2 5 7 9 : 2",
"3 5 11 19 : 12",
""
)

function exo(n) {
   efface();
   document.frm.t.value = Exemples[n];
   //cherche();
}

function hasard() {
  var s = "", k=0
  for(var i = 0; i<4; i++) {
     var u = Math.floor(1+6*Math.random());
     k += u;
     s = s + k+" ";
  }
  k =  Math.floor(1+30*Math.random());
  s = s + "; " + k;
  document.frm.t.value = s;
  document.frm.ecp.value = "";
}

