First look at the properties of logarithms, Loga (b^c) =c*loga (b), Loga (b*c) =loga (b) +loga (c);
Suppose given a number of 10234432, then log10 (10234432) =log10 (1.0234432*10^7) =log10 (1.0234432) +7;
LOG10 (1.0234432) is the small part of log10 (10234432). LOG10 (1.0234432) =0.010063744 10^0.010063744=1.023443198 so to take a few is very obvious bar ~ First take the logarithm (to 10), and then get the result of the number of parts Bit,pow (10.0,bit)
If the answer is still <1000 then always multiply by 10.
Note that I first processed the 0~20 item is to facilitate processing ~ This problem to use the formula of the series: an= (1/√5) * [((1+√5)/2) ^n-((1-√5)/2) ^n] (n=1,2,3 ...)
Take the logarithm log10 (AN) =-0.5*log10 (5.0) + ((double) n) *log (f)/log (10.0) +LOG10 (n ((1-√5)/(1+√5)) ^n) where f= (sqrt (5.0) +1.0)/2.0;
LOG10 ((1-√5)/(1+√5) ^n)->0 so can be written log10 (AN) =-0.5*log10 (5.0) + ((double) n) *log (f)/log (10.0);
Finally take its fractional part.
#include <iostream> #include <cmath> using namespace std;
int fac[21]={0,1,1};
Const double f= (sqrt (5.0) +1.0)/2.0;
int main () {double bit;
int n,i;
for (i=3;i<=20;i++) fac[i]=fac[i-1]+fac[i-2];//the first 20 items while (cin>>n) {if (n<=20) {
cout<<fac[n]<<endl;
Continue } bit=-0.5*log (5.0)/log (10.0) + ((double) n) *log (f)/log (10.0);//ignores the last infinitesimal bit=bit-floor (bit);
Bit=pow (10.0,bit);
while (bit<1000) bit=bit*10.0;
printf ("%d\n", (int) bit);
} return 0; }