/* rythm.js  -  Pavages ryhmiques parfaits -
 *
 * complémént du programme rythm.java de calcul des p.r.p.
 *
 * un pavage rythmique parfait est un :
 * recouvrement exact de l'ens. des k*n premiers naturels par n suites
 * arithmétiques finies de k termes et de raisons toutes différentes
 *
 * (ou partition de [0, k*n-1] en n parties de k éléments en progression
 * arithmétique, les raisons de ces progr. étant toutes différentes)
 *
 * (C) Jean-Paul Davalan Concarneau Décembre 2004 <jpdvl@wanadoo.fr>
 *
 * RYTHM.JS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * RYTHM.JS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with rythm.java; see the file COPYING.  If not, write to
 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */
var results = new Array(), sols,pos, maxsols=5, mode=0;
var sel;
var mus=new Array();

function prpr(s) {
     if(sols<= maxsols) {
       results[sols] = s;
       pos = sols;
       sols++;
     }
     document.frm.ar.value = s;
    
}

function rem(s) {
     document.frm.ar.value = s+"\n"+document.frm.ar.value;
}

var id;

function cherche() {
     var ns = parseInt(document.frm.n.value),
         ks = parseInt(document.frm.k.value),
	 v=1000*(1000*ns+ks)+100*maxsols+mode;
     if(ks<2) { 
       ks=2;
       document.frm.k.value=2;
     }
     if(ns<0) {
        ns =0;
        document.frm.n.value=0;
     }
     var s="Calcul en cours n= "+ns+"  k= "+ks+"\n"+
           "Arrêt après "+maxsols+" solutions au plus\n\n",
         s0= "Merci d'être patient !\n",
         s1= "Merci d'être extrêmement patient !\n",
         s2= "Votre patience n'a sûrement pas de limite !\n";
     if(ks <4 ) s += s0;
     else if(ks <5) s += s1;
     else s += s2;
     document.frm.ar.value = s;
     for(var j=0;j<10000;j++) ;
     sols=0;
     pos=0;
     document.R.rythm(v);
     if(ks >= 5) {
        id = setInterval("w()",10000);
     }
}

var cur=0;

var tab = new Array("Vous auriez plus de chance en jouant au loto", "Vous avez le temps de prendre un café", "Et aussi de faire les mots croisés","Puis de reprendre un autre café ...",
"Revenez voir dans deux ou trois jours, à tout hasard", "Ne vous endormez pas devant l'écran","J'espère que vous avez choisi un bon bouquin","Calculez mentalement sqrt(30+sqrt(13^2*25+675))","Quel temps fait-il chez vous ?","Le calcul de tout à l'heure a pour résultat 10","Vous aviez trouvé ?");

function w() {
  if(sols==0) {
    document.frm.ar.value=tab[cur]+"\n";
    cur = (cur+1)%tab.length;
  } else {
    clearInterval[id];
  }
}

function voir() {
  document.frm.ar.value = results[pos];
}

function suivant(k) {
  if(sols==0) return;
  pos += k;
  if(pos>=sols) pos = sols-1;
  if(pos<0) pos=0;
  voir();
}

function extr(r) {
  if(sols==0) return;
  if(r==0) pos=0;
  else if(r==1) pos = sols-1;
  voir();
}


var current = new String();
var max;

function selct() {
  max = 0;
  var s= new String(results[pos]);
  var t=s.split(/\n>\s/g);
  var current = t[1];
  var u = t[0].split(/:/g);
  var w, v = new String(u[1]);
  var or = new Array();
  var dec = new Array();
  var st = new String("\n");
  do {
    w = v;
    v = w.replace(/[\(\)]+/g,"");
  } while(w != v);
  var z = v.split(/;/g);
  for(var i=0;i<z.length;i++) {
    var r = z[i].split(/,/g);
    var a = parseInt(r[0]);
    or[a] = parseInt(r[1]);
    dec[a] = parseInt(r[2]);
    st += " ("+a+", "+r[1]+", "+r[2]+")"
    if(a>max)
      max=a;
  }
  current = current.replace(/\n/g,"");
  current = current.replace(/^\s/g,"");
  current = current.replace(/\s$/g,"");
  current = current.replace(/\s+/g," ");
  document.frm.ar2.value = current;
  var s = "\n";
  for(var i=0;i<=max;i++) {
    s += " ("+i+", "+or[i]+", "+dec[i]+")"
  }
  document.frm.ar2.value += st+s;
}

/*
function perm() {
  var i, max=0;
  var t= document.frm.ar2.value.split(/\s+/g);
  var u= new Array(), 
      or= new Array(), 
      dec= new Array(),
      p = new Array();

  for(i=0; i<t.length; i++) {
    if(u[t[i]]==void(0)) {
      if(t[i]>max) 
        max=t[i];
      p[t[i]] = new Array();
      p[t[i]][0] = i;
      or[t[i]]=i;
      u[t[i]]=1;
    } else if(u[t[i]]==1) {
    
      dec[t[i]]=i-or[t[i]];
      p[t[i]][1] = i;
      u[t[i]]=2;
      
    } else {
      
      p[t[i]][u[t[i]]] = i;
      if(dec[t[i]] != i - p[t[i]][u[t[i]]-1])
        alert("Erreur "+t[i]+" "+i);
      u[t[i]]++;
      
    }
  }

}
      
*/
/*

                                                      d'
                                                -c'- ----
                                             b
                                        -a- --- ---- ----
                                       g
 ------------------------------------f-------------------
                                   e
 --------------------------------d-----------------------
                               c
 ----------------------------B---------------------------
                           A
 ------------------------G-------------------------------
                       F
 --------------------E-----------------------------------
                   D
 ---- ---- ---- -C-
            B,
 ---- -A,-
  G,
*/
/*
function permute(n, tab) {
  for(i=0;i< n;i++) {
     b = Math.floor((n+1-i)*Math.rand())
     a = b+i;
     c = tab[a]
     tab[a] =  tab[i]
     tab[i] = c;
  }
}
function abc() {
var notes= new String("G, G,# A, A,# B, B,# C C# D D# E E# F F# G G# A A# B B# c c# d d# e e# f f# g g# a a# b b# c' c'# d' d'#");
var tnotes = notes.split(/\s/g), mil = Math.floor(tnotes.length/2);

}
function lil() {

}
*/

