0

I have a new question regarding a Fibonacci sequence that is kinda unique. It involves two input M and N and looks like this:

Example :

2-Fibonacci sequence: {1, 1, 2, 3, 5, 8, 13, ...} 
3-Fibonacci sequence: {1, 1, 2, 4, 7, 13, 24, ...}

I need to code a Python function that can print out the first M elements of an N-Fibonacci sequence.

For example:

Input → N=3; M=5 Output →1 1 2 4 7
Input → N=2; M=7 Output →1 1 2 3 5 8 13

I would really appreciate any help

user3837868
  • 917
  • 1
  • 12
  • 24
codingnoob
  • 39
  • 1
  • 3
  • 5
    Interesting problem! But... what have you tried? To help you get started, try writing something for a 2-fibonacci sequence first, then generalise to a n-fib sequence. – TrebledJ May 24 '19 at 13:55
  • 7
    Since this seems like a homework problem, I would recommend reading [this](https://meta.stackoverflow.com/a/334823/10366273)... the main point being that we aren't here to solve the problem from scratch for you but if you attempt to implement a solution and it doesn't work we can help you figure out what's wrong with what you did so far and how to improve it. – MyNameIsCaleb May 24 '19 at 13:58

3 Answers3

0
n = int(input("sequence: "))
m = int(input("amount of numbers: "))
numbers = [1, 1]
for i in range(n-2):
    numbers.append(sum(numbers))
for num in numbers:
    print(num)
for i in range(m-len(numbers)):
    numbers.append(sum(numbers))
    print(numbers[-1])
    numbers.pop(0)

If i understood you correctly this is what you are looking for.

Challe
  • 599
  • 4
  • 19
  • If you are using `append` and `pop` on a `list` than that is a hint you should be using `deque`. – Error - Syntactical Remorse May 24 '19 at 14:45
  • @Error-SyntacticalRemorse Is it someway more efficient to use deque? To me it seems just more complicated. – Challe May 24 '19 at 15:02
  • A `list` is not designed to be constantly grown and retracted (i.e. it is not designed to be treated as a queue or stack). If you use it that way you consume extra memory and time. A `deque` is specifically designed for that reason. A `list` is good for storing (i.e. insertion, sorting, and iterating over). – Error - Syntactical Remorse May 24 '19 at 15:04
  • @Error-SyntacticalRemorse what do you base your claim on? If im understanding this python source code correctly ([link](https://github.com/python/cpython/blob/master/Modules/_collectionsmodule.c "collections source")) `deque` uses `Doubly Linked List`. And according to this Stackabuse article [link](https://stackabuse.com/doubly-linked-list-with-python-examples/ "stackabuse article") use more memory and aren't faster to use. – Challe May 24 '19 at 18:07
  • My bad. Speed not space (though deque does not use much more space. See [Deque in Python](https://www.geeksforgeeks.org/deque-in-python/) or [the official documentation](https://docs.python.org/2/library/collections.html#collections.deque). For rapid pushing and popping is the exact reason `deque` was created for python. – Error - Syntactical Remorse May 24 '19 at 18:13
0

Because it's a fun problem to do with a generator here is a generator solution:

from collections import deque
def fib(return_size, last_x=2):
    def fib_gen(sum_last_x_elements):
        last = deque(maxlen=sum_last_x_elements)
        last.append(1) # Prime our fib sequence
        while True:
            yield last[-1] # peek at the right item and return it
            last.append(sum(last)) # Sum up our queue and add it to end
    gen = fib_gen(last_x) # Create generator
    return [next(gen) for _ in range(return_size)] # Create return list.

print(fib(9, 3)) # Output: [1, 1, 2, 4, 7, 13, 24, 44, 81]
0

Instead of managing two variables (a,b) and adding them up, use the resulting list itself and sum the last N items to add each new one:

def fibo(N,M):
    result  = [1]
    for i in range(M-1):
        result.append(sum(result[-N:]))
    return result

You could generalize this even more by accepting a function or lambda that calculates the next value based on the series so far:

def series(N,getNext,init=[1]):
    result  = init
    for i in range(N-len(init)):
        result.append(getNext(result))
    return result

series(10,lambda r:sum(r[-2:]))             # [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
series(10,lambda r:sum(r[-2:]),init=[5,-3]) # [5, -3, 2, -1, 1, 0, 1, 1, 2, 3]
series(10,lambda r:sum(r[-3:]))             # [1, 1, 2, 4, 7, 13, 24, 44, 81, 149]
series(10,lambda r:r[-1]*2)                 # [1, 2, 4, 8, 16, 32, 64, 128, 256, 512]
series(10,lambda r:r[-2]-r[-1],init=[-1,1]) # [-1, 1, -2, 3, -5, 8, -13, 21, -34, 55]
Alain T.
  • 40,517
  • 4
  • 31
  • 51