Finding the closest fibonacci numbers

前端 未结 11 2144

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:04

    On Scala find the closet Fibonaci number looks also very simple:

    val phi = (1 + sqrt(5))/2
    def fib(n: Long): Long =  round( ( pow(phi,n) -  pow((1-phi),n) ) / sqrt(5)  )
    def fibinv(f: Long): Long =  if (f < 2) f else round( log(f * sqrt(5) ) /log(phi))
    

提交回复
热议问题