Create faster Fibonacci function for n > 100 in MATLAB / octave

前端 未结 8 1859
难免孤独
难免孤独 2020-12-31 11:34

I have a function that tells me the nth number in a Fibonacci sequence. The problem is it becomes very slow when trying to find larger numbers in the Fibonacci sequence doe

8条回答
  •  旧时难觅i
    2020-12-31 11:46

    Seems like fibonaacci series follows the golden ratio, as talked about in some detail here.

    This was used in this MATLAB File-exchange code and I am writing here, just the esssence of it -

    sqrt5 = sqrt(5);
    alpha = (1 + sqrt5)/2;   %// alpha = 1.618... is the golden ratio
    fibs  = round( alpha.^n ./ sqrt5 )
    

    You can feed an integer into n for the nth number in Fibonacci Series or feed an array 1:n to have the whole series.

    Please note that this method holds good till n = 69 only.

提交回复
热议问题