def fib(n):(adsbygoogle = window.adsbygoogle || []).push({});

f0, f1, = 0, 1

for i in range(n - 1):

f0, f1 = f1, f0 + f1

return f1

It looks like it'd be linear, given there's only one loop, but when I plotted n against runtime, the relationship was quadratic, why?

**Physics Forums - The Fusion of Science and Community**

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# Why is the complexity of this code O(n^2)?

Loading...

Similar Threads - complexity code | Date |
---|---|

What should be the rules to write excellent code? | Mar 12, 2018 |

Java Help-Improving runtime complexity of the method | Jan 6, 2017 |

How to interpret a complex Matrix as a Probability Matrix? | Nov 22, 2016 |

Solving complex PDEs | May 25, 2016 |

Why is the complexity class expression this? | Jan 29, 2016 |

**Physics Forums - The Fusion of Science and Community**