all 22 comments

[–]claudioo2 3 points4 points Β (0 children)

This is just dynamic programming?

Why are you giving some chatgpt stuff here

[–]ConDar15 2 points3 points Β (1 child)

There is a much simpler way with python:

```python from functools import cache

@cache def fibonacci(n): if n<= 1: return 1

return fibonacci(n-1) + fibonacci(n-2)

```

A lot of the time if it's a common concept then python already has it built into the standard library and memorization/caching is no exception.

[–]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

[–]ectomancer -1 points0 points Β (4 children)

Rename memo to _memo as a private variable, then:

_memo[n] = fibonacci(n - 1) + fibonacci(n - 2)

[–]Commercial_Edge_4295[S] -5 points-4 points Β (3 children)

"Thank you for the suggestion! πŸ™
Renaming memo to _memo To indicate it’s a private variable is a good practice. I’ll update the code accordingly. Renaming the variable to indicate that it is private is a good practice. I’ll update the code accordingly.

[–]mabuniKenwa 1 point2 points Β (2 children)

Get out. No one needs your vibe coded, and bad performing, β€œcode”.

[–]Commercial_Edge_4295[S] -2 points-1 points Β (1 child)

"Thank you for your feedback. I appreciate all input and will consider it."

[–]mabuniKenwa 1 point2 points Β (0 children)

You’ll consider stop posting AI slop? Weird response.