[ToJ 3600] Fibonacci II (logarithm + Fibonacci)

Source: Internet
Author: User

Description

2007 is coming. After one year of practice in 2006, zouyu, a mathematical prodigy, finally ranked 0 to 100000000 In the Fibonacci series.
(F [0] = 0, F [1] = 1; F [I] = f [I-1] + F [I-2] (I> = 2 )) all the values are backed up.
Next, codestar decided to test him, so every time he asked him a number, he would say the answer, but some numbers are too long. Therefore, it is required that only the first four digits can be said, but codestar cannot remember it. So he decided to write a program to test whether zouyu was correct.

Input

Enter several numbers N (0 <= n <= 100000000), one row for each number. Read the end of the file.

Output

Output the first four digits of F [N] (if there are less than four digits, all are output ).

Sample Input

0
1
2
3
4
5
35
36
37
38
39
40

Sample output

0
1
1
2
3
5
9227
1493
2415
3908
6324
1023

Question:

Assume that a number of 10234432 is given, then log10 (10234432) = log10 (1.0234432*10 ^ 7) =Log10 (1.0234432)+ 7.
Log10 (1.0234432)=0.010063744This is the fractional part of log10 (10234432.
So 10 ^0.010063744= 1.023443198. The first four digits of 1023 are the answer!

For easy computing, I have preprocessed the first 17 Fibonacci numbers here.

 

While using the logarithm, this question also requires the generic formula of the Fibonacci series:

Use the logarithm of 10 for this formula.

Because the limit of log10 (1-(1-√ 5)/(1 + √ 5) ^ N is 0 when n is infinitely increased, therefore, we can save this item when writing a formula.

#include<bits/stdc++.h>using namespace std;int fib[20];int main(){    int i,j,k,n;    fib[0]=0;    fib[1]=1;    for(i=2;i<=17;i++)        fib[i]=fib[i-1]+fib[i-2];    double t=(1.0+sqrt(5))*0.5,ans;    while(scanf("%d",&n)!=EOF)    {        if(n<=17)            printf("%d\n",fib[n]);        else        {            ans=-0.5*log10(5.0)+n*1.0*log10(t);            ans=ans-floor(ans);            ans=pow(10.0,ans);            while(ans<1000)                ans*=10;            printf("%d\n",int(ans));        }    }    return 0;} 

 

[ToJ 3600] Fibonacci II (logarithm + Fibonacci)

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.