求Fibonacci数列的高四位

先看对数的性质,loga(b^c)=c*loga(b),loga(b*c)=loga(b)+loga(c);

假设给出一个数10234432,那么log10(10234432)=log10(1.0234432*10^7)=log10

(1.0234432)+7;

log10(1.0234432)就是log10(10234432)的小数部分.

log10(1.0234432)=0.010063744

10^0.010063744=1.023443198

那么要取几位就很明显了吧~

先取对数(对10取),然后得到结果的小数部分bit,pow(10.0,bit)以后如果答案还是

<1000那么就一直乘10。

注意偶先处理了0~20项是为了方便处理~

这题要利用到数列的公式:an=(1/√5) * [((1+√5)/2)^n-((1-√5)/2)^n]

(n=1,2,3.....)

 

这个题目就是用到这个公式,化简f(n)=n*log10((1+sqrt(5))/2)-log10(sqrt

(5))+log10(1-((1-sqrt(5))/(1+sqrt(5)))^n)后面红色部分是无穷小量,可以省略

于是f(n)=n*log10((1+sqrt(5))/2)-log10(sqrt(5));

#include<stdio.h>

#include<math.h>

int f[21]={0,1,1};

int main()

{    

int n,i;    

for(i=2;i<21;++i)        

f[i]=f[i-1]+f[i-2];    

while(scanf("%d",&n)!=EOF)    

{         if(n<=20)        

{            

printf("%d\n",f[n]);            

continue;        

}        

else       

{            

double temp=-0.5*log(5.0)/log(10.0)+((double)n)*log((sqrt

(5.0)+1.0)/2.0)/log(10.0);            

temp-=floor(temp);            

temp=pow(10.0,temp);            

while(temp<1000)                

temp*=10.0;            

printf("%d\n",(int)temp);        

}    

}

//system("pause");

return 0;

}


作者:​​火星十一郎​