

//	chiffreAt( "123456", 0) donne le chiffre de droite 6 sous forme de nombre
//	contrairement à charAt qui part de la gauche et qui donne une chaîne d'un
//	seul caractère

function chiffreAt(s, t) {
	s += ""
	var l = s.length
	return  (t < l) ? parseInt(s.charAt(l-t-1)) : 0
}

//alert(chiffreAt("123456789",1))
/*
	s est une chaîne de chiffres représentant un nombre entier S
	n est un entier
	retourne   S x n sous forme de chaîne
*/
var Pow = []

function mult(s, n) {
	var r,l,x,c,st;
	r = 0   // retenue
	l = s.length
	st =""
	for(var i= 0; i< l || r != 0; i++) {
		x = n * chiffreAt(s,i) + r
		c = x% 10
		r = (x-c)/10
		st = c + st
	}
	return st
}


function add(s0, s1) {
	var l0,l1,st,r,x,c
	l0 = s0.length
	l1 = s1.length
	st = ""
	r=0
	for(var i=0; i<l0 || i<l1 || r != 0; i++) {
		x = chiffreAt(s0,i) + chiffreAt(s1,i) + r
		c = x%10
		r = (x-c)/10
		st = c + st
	}
	return st
}

//alert(mult(add("1234567890123456789","123456012345678901234567890123456789"), 7))

function init() {
	var n, x
	for(var i=0; i<10; i++) {
		Pow[i]=new Array();
	}
	for(var i=0; i<=9; i++) {
		Pow[i][0] = "1"
		Pow[i][1] = i+""
	}
	for(var x=1; x<100; x++) {
		Pow[0][x] = "0"
		Pow[1][x] = "1"
	}
	for(i=2; i<9; i++)
		for(var n=2; n<=30; n++)
			pow(i, n)
}


function pow(x, e) {
	if(Pow[x]==null) {
		Pow[x] = []
	}
	if(Pow[x][e] == null) {
		if(x==0 || x==1 || e==1) {
         	       Pow[x][e] = x
        	} else {
			Pow[x][e] =  mult(pow(x, e-1), x)
		}
	}
	return Pow[x][e]
}

function suivt(s) {
	var l = s.length, st = "", p
	for(var i=0; i<l; i++) {
		p = pow(chiffreAt(s, i), l)
		st = add(st, p)
	}
	return st;
}

function hash(s) {
	return s.substr(0,4)
}

var Hauteur = [], Periode=[], Ultime=[]
init()


var trajectoire = function(s) {
	this.current=0
	this.str = ""
	this.traj = []
	this.rang = []
	this.prochain = s+""
	this.duree = 0
	this.periode= 0
	this.etat = "avance"
	this.elt = ""
	this.end=""
	this.add = function() {
		var s = this.prochain
		if(this.rang[s]==null) {
			this.traj[this.current]= s
			this.rang[s] = this.current
			this.current++
			document.getElementById("volID").innerHTML =this.prochain
			this.etat = "avance"
			this.str += s+" "
			this.prochain = suivt(s)
		} else {
			this.str += "&rarr; " + s
			var t = this.rang[s]
			this.end = s
			this.duree = t
			this.periode = this.current-t
			this.etat = "fin"
		}
	}
	this.affiche = function() {
		this.str1=""
		this.str2=""
		for(var i=0; i<this.current; i++) {
			var ch = this.traj[i]
			if(i == this.duree) {
				this.str2 += '<span style="color:red;font-weight:bold;font-size:15px;">('+ch+")</span> &rarr; "
			} else {
				this.str2 += ch+" &rarr; "
			}
			if(ch.length>=50 || this.str2.length>80) {
				this.str2 +="<br />"
				this.str1 += this.str2
				this.str2=""
			}
		}
		if(this.str2 !="")
			this.str1 += this.str2
		this.str1 += " " + '<span style="color:red;font-weight:bolder;font-size:15px;">('+this.end+")</span> "
		this.str1 += '<br /><span style="font-weight:bold;">Durée du vol :</span> '+ this.duree +"<br />"
		if(this.periode==1) {
			this.str1 += '<span style="font-weight:bold;">Le vol atteint le point fixe</span> '+this.traj[this.duree]+"<br />"
		} else {
			this.str1 += '<span style="font-weight:bold;">Le vol atteint un cycle de période</span> '+this.periode+" au point "+this.traj[this.duree]+"<br />"
		}
		document.getElementById("volID").innerHTML = this.str1
	}
		
}

var Traj, IDtraj, drapeau=false;
var msec = 1;

function vol(s) {
	drapeau=false;
	var l = Math.floor(s.length/50)
	msec = (l>1) ? l : 1;
	if(Traj != null) {
		delete(Traj)
	}
	if(IDtraj!=null) {
		clearInterval(IDtraj)
	}
	Traj = new trajectoire(s)
	IDtraj = setInterval("envol();",msec);
}
	
function envol() {
	if(Traj==null || drapeau==true) return;
	if(Traj.etat == "fin") {
		clearInterval(IDtraj)
		Traj.affiche()
	} else {
		drapeau=true
		Traj.add()
		drapeau=false
	}
}


function didCherche () {
	var x = new String(document.didForm.N.value)
	x = x.replace(/^\s*0+/,"")
	x = x.replace(/\s+$/,"")
	if(x=="") x="0"
	document.didForm.N.value = x
//alert(exp)
	//var x = Math.floor(eval(exp))
//alert(exp+" = "+x)
	var s = didact(x)
	document.getElementById("didID").innerHTML=s
}

var didSuivant="";
//alert(pow(8, 3))
function transf(x) {
	var l= x.length
	var s = ""
	for(var i=0; i<l; i++) {
		s += ((i==0)? "T(N) = " : " + ") + x.charAt(i)+"<sup>"+l+"</sup>"
	}
	s += "<br />"
	for(var i=0; i<l; i++) {
		var u = parseInt(x.charAt(i))
		var v = pow(u, l)
		s += ((i==0)?"T(N) = " : " + ") + v
	}
	s += "<br />"
	var s1 = suivt(x)
	didSuivant = s1;
	s += ((l==1)? "" : ("T(N) = " + s1 + "<br />"))
	if(s==s1) { 
		s += x + " est un nombre narcissique.<br />"
	}
	return s;
}

function didact(x) {
	x = x+"";
	var s = "Le nombre est N = "+x+"<br />"
	var n = x.length;
	s += "N a "+n+" chiffre"+((n>1)?"s":"")+"<br />"
	s += transf(x)
	return s
}

function didHasard() {
	var n =1+Math.floor(90*Math.random());
	var s = new String()
	s = ""
	s +=(1+Math.floor(9*Math.random()));
	for(i=0;i<n; i++)
		s += ""+Math.floor(10*Math.random())
	document.getElementById("didID").innerHTML=""
	document.didForm.N.value = s
	didSuivant=""
	didCherche()
}

function didEfface() {
	document.getElementById("didID").innerHTML=""
	document.didForm.N.value = ""
	//didSuivant = ""
}


function didSuivt() {
	if(didSuivant!=null && didSuivant!="") {
		didEfface();
		document.didForm.N.value = didSuivant
		didCherche();
	} else {
		didCherche();
		didSuivt();
	}
}
function didNarcisse() {
	var a = Math.floor(narcisses.length*Math.random());
	document.didForm.N.value = narcisses[a];
	didCherche();
}

function didCycle() {
        var a = Math.floor(cyclesTab.length*Math.random());
        document.didForm.N.value = cyclesTab[a];
        didCherche();
}


function volEfface() {
        drapeau=false;
        if(Traj != null) {
                delete(Traj)
        }
        if(IDtraj!=null) {
                clearInterval(IDtraj)
        }
        document.getElementById("volID").innerHTML=""
        document.volForm.N.value = ""
}

function volCherche () {
        var x = new String(document.volForm.N.value)
        x = x.replace(/^\s*0+/,"")
	x = x.replace(/\s+$/,"")
        if(x=="") x="0"
        document.volForm.N.value = x

        vol(x)
}

function volHasard() {
        var n =2+Math.floor(200*Math.random());
        var s = new String()
        s = ""
        s +=(1+Math.floor(9*Math.random()));
        for(i=0;i<n; i++)
                s += ""+Math.floor(10*Math.random())
        document.getElementById("volID").innerHTML=""
        document.volForm.N.value = s
        volCherche()
}

function volCycle() {
        var a = Math.floor(cyclesTab.length*Math.random());
        document.volForm.N.value = cyclesTab[a];
        volCherche();
}
function excalc(x) {
	document.didForm.N.value=x;
	document.location.href="#CALC";
	didCherche();
}

function exvol(x) {
	document.volForm.N.value=x
	//document.location.href.replace("#VOL")
	document.location.href="#VOL"
	volCherche();
}

function volLong() {
	if(longCheminTab==null) return
	var a = Math.floor(longCheminTab.length*Math.random());
	document.volForm.N.value = longCheminTab[a];
        volCherche();
}

