Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

program to find fibonacci sequence of a number

how do i find the fibonacci sequence of a number . Here Is The code

def fib(n):
    for i in range(n):
        b = 1 
        
        b+=i
        print(b)
        
p = fib(9)

the program just returns the normal sum. how to do this in the easy way

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The fibonacci sequence is built by adding the last two values of the sequence, starting with 1,1 (or 0,1 for the modern version). A recursive function does it elegantly:

def fib(n,a=1,b=1): 
    return a if n==1 else fib(n-1,b,a+b)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading