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

前端 未结 8 1819
难免孤独
难免孤独 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条回答
  •  被撕碎了的回忆
    2020-12-31 11:50

    To reach large numbers you can use symbolic computation. The following works in Matlab R2010b.

    syms x y %// declare variables
    z = x + y;  %// define formula
    xval = '0'; %// initiallize x, y values
    yval = '1'; 
    for n = 2:300
        zval = subs(z, [x y], {xval yval}); %// update z value
        disp(['Iteration ' num2str(n) ':'])
        disp(zval)
        xval = yval; %// shift values
        yval = zval;
    end
    

提交回复
热议问题