How to write the Fibonacci Sequence?

前端 未结 30 2360
醉酒成梦
醉酒成梦 2020-11-22 00:32

I had originally coded the program wrongly. Instead of returning the Fibonacci numbers between a range (ie. startNumber 1, endNumber 20 should = only those numbers between 1

相关标签:
30条回答
  • 2020-11-22 01:21

    How about this one? I guess it's not as fancy as the other suggestions because it demands the initial specification of the previous result to produce the expected output, but I feel is a very readable option, i.e., all it does is to provide the result and the previous result to the recursion.

    #count the number of recursions
    num_rec = 0
    
    def fibonacci(num, prev, num_rec, cycles):
    
        num_rec = num_rec + 1
    
        if num == 0 and prev == 0:
            result  = 0;
            num = 1;
        else:
            result = num + prev
    
        print(result)
    
        if num_rec == cycles:
            print("done")
        else:
            fibonacci(result, num, num_rec, cycles)
    
    #Run the fibonacci function 10 times
    fibonacci(0, 0, num_rec, 10)
    

    Here's the output:

    0
    1
    1
    2
    3
    5
    8
    13
    21
    34
    done
    
    0 讨论(0)
  • 2020-11-22 01:23

    These all look a bit more complicated than they need to be. My code is very simple and fast:

    def fibonacci(x):
    
        List = []
        f = 1
        List.append(f)
        List.append(f) #because the fibonacci sequence has two 1's at first
        while f<=x:
            f = List[-1] + List[-2]   #says that f = the sum of the last two f's in the series
            List.append(f)
        else:
            List.remove(List[-1])  #because the code lists the fibonacci number one past x. Not necessary, but defines the code better
            for i in range(0, len(List)):
            print List[i]  #prints it in series form instead of list form. Also not necessary
    
    0 讨论(0)
  • 2020-11-22 01:24

    Using for loop and print just the result

    def fib(n:'upto n number')->int:
        if n==0:
            return 0
        elif n==1:
            return 1
        a=0
        b=1
        for i in range(0,n-1):
            b=a+b
            a=b-a
        return b
    

    Result

    >>>fib(50)
    12586269025
    >>>>
    >>> fib(100)
    354224848179261915075
    >>> 
    

    Print the list containing all the numbers

    def fib(n:'upto n number')->int:
        l=[0,1]
        if n==0:
            return l[0]
        elif n==1:
            return l
        a=0
        b=1
        for i in range(0,n-1):
            b=a+b
            a=b-a
            l.append(b)
        return l
    

    Result

    >>> fib(10)
    [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
    
    0 讨论(0)
  • 2020-11-22 01:25

    Fibonacci sequence is: 1, 1, 2, 3, 5, 8, ....

    That is f(1) = 1, f(2) = 1, f(3) = 2, ..., f(n) = f(n-1) + f(n-2).

    My favorite implementation (simplest and yet achieves a light speed in compare to other implementations) is this:

    def fibonacci(n):
        a, b = 0, 1
        for _ in range(1, n):
            a, b = b, a + b
        return b
    

    Test

    >>> [fibonacci(i) for i in range(1, 10)]
    [1, 1, 2, 3, 5, 8, 13, 21, 34]
    

    Timing

    >>> %%time
    >>> fibonacci(100**3)
    CPU times: user 9.65 s, sys: 9.44 ms, total: 9.66 s
    Wall time: 9.66 s
    

    Edit: an example visualization for this implementations.

    0 讨论(0)
  • 2020-11-22 01:25

    use recursion:

    def fib(n):
        if n == 0:
            return 0
        elif n == 1:
            return 1
        else:
            return fib(n-1) + fib(n-2)
    x=input('which fibonnaci do you want?')
    print fib(x)
    
    0 讨论(0)
  • 2020-11-22 01:28

    Basically translated from Ruby:

    def fib(n):
        a = 0
        b = 1
        for i in range(1,n+1):
                c = a + b
                print c
                a = b
                b = c
    

    ...

    0 讨论(0)
提交回复
热议问题