Jump to content
Webz

Fibonacci recursion demo PY. ( GIF )

Recommended Posts

4 hours ago, Byte-ul said:

Cum crezi ca e mai bine sa implementezi acest algoritm? Recursiv sau iterativ?

Recursiv, dar utilizezi un cache ca sa salvezi rezultatele.

Astfel la o apelare ulterioara timpul de executie scade foarte mult xD.

Link to comment
Share on other sites

 

def fib(n):
    v1, v2, v3 = 1, 1, 0
    for rec in bin(n)[3:]:
        calc = v2*v2
        v1, v2, v3 = v1*v1+calc, (v1+v3)*v2, calc+v3*v3
        if rec=='1':    v1, v2, v3 = v1+v2, v1, v2
    return v2

 

Edited by sharkyz
Link to comment
Share on other sites

On 4/5/2016 at 2:11 AM, sharkyz said:

 


def fib(n):
    v1, v2, v3 = 1, 1, 0
    for rec in bin(n)[3:]:
        calc = v2*v2
        v1, v2, v3 = v1*v1+calc, (v1+v3)*v2, calc+v3*v3
        if rec=='1':    v1, v2, v3 = v1+v2, v1, v2
    return v2

 

 

 

Asta-i metoda tricky ? :))

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...