Finding the closest fibonacci numbers

前端 未结 11 2181

I am trying to solve a bigger problem, and I think that an important part of the program is spent on inefficient computations.

I need to compute for a given number N, th

11条回答
  •  花落未央
    2021-02-02 14:15

    I just did a CodeChef puzzle that was this exact problem (http://www.codechef.com/problems/DPC204). I simply calculated the Fibonacci sequence from 0 to the end of the range, and counted how many were after the beginning of the range. My test for whatever their sample inputs were took 2.6M, and 0.00s, so the nieve solution is plenty fast enough.

    Basically, I made a big-unsigned-int class made of unsigned int[333], and calculate two numbers per loop, to avoid swaps.

    start with A=0,B=1;
    A+=B;B+=A; 
    now A==1,B==2, the next two Fib. numbers, with no swaps.
    A+=B;B+=A; 
    now A==3,B==5, the next two Fib. numbers, with no swaps.
    

    It is slightly complicated by the fact you have to stop and check if the neither, one, or both numbers are in the range, but A

    My solution on CodeChef clocked in at 0.00 seconds, so I think this method ought to be fast enough, you just have to write a function that adds one uint[333] to another uint[333] (using all 32 bits, just chars for each decimal digit)

提交回复
热议问题