fibonacciアルゴリズム

4295 ワード

int hi,lo;



hi=lo=1;
hi=hi+lo; //2 3 5 8 13 ……



lo=hi-lo;


 
unsigned int Fib1(unsigned int n)
{
    if (n == 1 || n == 2)        
           return 1;    
   else        
           return Fib(n - 1) + Fib(n - 2);
}
unsigned
int Fib2(unsigned int n)
{    
    unsigned int nRet, nP, nPp;
nRet
= nP = nPp = 1;

if ((n == 1) || (n == 2))
return nRet;

for (unsigned int i = 3; i <= n; i++)
{
nRet
= nP + nPp;

nPp
= nP;
nP
= nRet;
}

return nRet;
}