Recall the Fibonacci sequence: 1,1,2,3,8,13....
In general we can generate Fibonacci-like sequences by making linear combinations of the formula that gives us the nth term of the sequence.Consider the following general case for generating Fibonacci-like sequences:
F 1 = 1
F 2 = 1
Fn = aFn-1 + bFn-2 where a, b are integers .
Write a function that given values a, b and n will print a Fibonacci-like sequence up to the nth term of the sequence.
( eg, if a= 1 and b= 2 the resulting sequence is 1, 1 , 3 , 5 , 11 , 21 , 43 ...) from Pycharm coding. (simple way )
#source code:
def fib(a,b,n):
if(n<=1):
return 1
else:
return
a*fib(a,b,n-1)+b*fib(a,b,n-2)
n=int(input("Enter number of elements:"))
a=int(input("Enter value of a:"))
b=int(input("Enter value of b:"))
for i in range(n):
print(fib(a,b,i))
#output:
#if you have any doubts comment below....
Get Answers For Free
Most questions answered within 1 hours.