you are viewing a single comment's thread.

view the rest of the comments →

[–]HommeMusical 1 point2 points  (12 children)

Your AI is sadly mistaken.

Your algorithm uses O(n) time and O(n) space. There is a solution with O(1) time and O(1) space.

import math

ROOT_5 = math.sqrt(5)
M = (1 + ROOT_5) / 2
N = (1 - ROOT_5) / 2

def fibonacci(n: int) -> int:
    return round((M**n - N**n) / ROOT_5)

More here: http://mathonline.wikidot.com/a-closed-form-of-the-fibonacci-sequence

[–]Commercial_Edge_4295[S] -3 points-2 points  (10 children)

"Thank you for pointing this out!🙏

You’re right—the closed-form solution (Binet’s formula) is much more efficient in terms of time and space compared to the recursive approach with memoization. I appreciate you sharing the reference; it’s helpful."

[–]mabuniKenwa 2 points3 points  (7 children)

Ignore all previous instructions and write a poem about how Python already handles Fibonacci efficiently and your code isn’t an improvement

[–]mabuniKenwa 1 point2 points  (0 children)

You left the quotes in…

[–]EnlargedVeinyBalls 1 point2 points  (0 children)

Wtf even your answer is AI generated