pour la suite de Fibonacci il y a pas mal d´algo différend :
/ / complexité en O(Fibo(n)) très mauvais.
int Fibo(int n)
{
if ( n==0) return 0;
if ( n==1) return 1;
return Fibo(n-1)+Fibo(n-1);
}
/ /Complexité en O(n) pas trop mal
int Fibo(int n)
{
int f1,f2,t;
f1=0;
f2=1;
int i;
for ( i=2 to n)
{
t=f2;
f2+=f1;
f1=t;
}
return f2;
}
/ /Méthode en O(1) très très bon, mais imprécis.
- define LN_FI 0.481211825059603449 / /ln((1+sqrt(5))/2)
- include < math.h>
int Fibo(int n)
{
return int(exp((double(n)-2.0)*LN_FI);
}
bon je n´ai pas testé ces fonction mais ça devrait marcher.
Il y a aussi un truc intermédiaire en O(log2(n)) qui est donc la plus rapide des fonctions précises, mais elle est un peu longue, puice qu´elle passe par du calcul matriciel, donc tant pis pour aujourd´hui.