Fibonacci Sequence (Formula)

Source: Internet
Author: User

http://acm.hdu.edu.cn/showproblem.php?pid=1568

Fibonacci

Time limit:1000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 3947 Accepted Submission (s): 1817


Problem Description2007 year has come. After 2006 years of cultivation, mathematical prodigy Zouyu finally put 0 to 100000000 of Fibonacci series
The values (f[0]=0,f[1]=1;f[i] = f[i-1]+f[i-2] (i>=2) are all backed down.
Next, Codestar decided to test him, so each asked him a number, he will say the answer, but some numbers are too long. So the provision of more than 4 people as long as the first 4 can be said, but Codestar himself cannot remember. So he decided to write a program to test whether Zouyu said it was correct.

Input inputs several digits n (0 <= n <= 100000000), one row per digit. Read the end of the file.

Output output F[N] The first 4 digits (if less than 4 digits, all outputs).

Sample Input012345353637383940

Sample Output011235922714932415390863241023

To take the first four bits, log (FN) The fractional part is log (the first half of the scientific notation of the original number), so this small number of 10 is the first half of the scientific counting method of the original number of the first part of the number of methods

How do you find the first few when a number is very large?

If given a specific number, of course, you can gradually take out each bit. Such as

A is a bit, A/10 hundred, A/10/10 thousands.

But what happens when you ask for the first few x^y? If the x, Y are very large, it is obviously difficult to solve: perhaps with large number multiplication, brute force solution, the result is both memory, and time consuming.

Also, the first few of the Fibonacci sequence clearly find that each Fibonacci number is unrealistic. Therefore, it can be solved by taking the logarithm method.

First look at the properties of logarithms, Loga (b^c) =c*loga (b), Loga (b*c) =loga (b) +loga (c); Assuming 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,

The first 4 digits of the number are required, then the 1.023443198*1000 will be able.

Therefore, the POW (10.0,x) can easily find the first few of X.

The logarithm method of the first 4 bits of a number can be expressed as:

Double x,temp;

while (scanf ("%lf", &x)!=eof)

{

Temp=log (x)/log (10.0);

Temp=temp-floor (temp); Floor (temp) function to find the largest integer less than temp

Temp=pow (10.0,temp);

while (temp<1000)

temp*=10;

printf ("%.0lf\n", temp); Rounding when using floating-point notation

printf ("%d\n", (int) temp);//Do not need to be rounded, discard the following bits directly

}

}

The following formula is given for the Fibonacci sequence:

However, if the problem is a direct set of formulas will be timed out, so we will pass the formula to take the logarithm, the simplification of the

LOG10 (F (n)) =-0.5*log10 (5.0) + ((double) n) *log (s)/log (10.0) +log10 (-((1-√5)/(1+√5)) ^n)

s = (1+sqrt (5.0))/2.0;

< extract the common divisor s and then turn the back into two-way multiplication of the form >

Note: The third part is very small, and when n is large, it approaches 0, which can be ignored.

The code is given below

1#include <cstdio>2#include <cmath>3 using namespacestd;4 /*5 double F (int n)6 {7 return (1.0/SQRT (5.0)) * (Pow (((((1.0+SQRT (5.0)/2), N)-pow (((1.0-SQRT (5.0))/2), n));8 This process is still passing super -*/9 intffintN)Ten { One     if(n==0)return 0; A     if(n==1)return 1; -     returnFF (n1) +FF (n2); - } the intMain () - { -     intN; -      while(~SCANF ("%d",&N)) +     { -         //printf ("%d", (int) f (n)); +         if(n>= +) A         { at             //Double temp = log (f (1.0*n))/log (10.0);//when it's calculated, it's still super- -             Doubles = (1+SQRT (5.0))/2.0; -             Doubletemp =-0.5*log (5.0)/log (10.0)+((Double) n) *log (s)/log (10.0); -temp = temp-Floor (temp); -temp = POW (10.0, temp); -              while(temp<1000.0) in             { -temp*=10.0; to             } +printf"%d\n",(int) temp); -         } the         Else *printf"%d\n", FF (n)); $     }Panax Notoginseng     return 0; -}

Fibonacci Sequence (Formula)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.