In: Computer Science
PLEASE WRITE IN PYTHON A sequence of integers is said to be Fibonacci-like if each element of the sequence (except the first two elements) is the sum of the previous two integers in the sequence. For example, the sequence 10, 14, 24, 38, 62, 100, 162, 262 is Fibonacci-like. Note that the first two integers in the above sequence are arbitrary. Each of the remaining integers is the sum of the two integers just before it in the sequence. For example, 24 = 10 + 14, 38 = 14 + 24, 62 = 24 + 38, and so on. Your program must query ‘‘Enter the sequence.’’. The user will input integers separated by a ’,’. There could be arbitrary amount of blank space between two successive numbers. Your program must read in the user input and determine if the sequence is Fibonacci-like or not by outputting respectively, ‘‘The sequence is Fibonacci-like.’’ or ‘‘The sequence is NOT Fibonacci-like.’’
********************************************** CHECKING FIBONACCI SEQUENCE*************************************************
Fibonacci Sequence : seq[ i ] , seq[i+1] , seq[ i+2] , seq[i+3] , ......seq[ last term]
Condition : seq[ i ] + seq[ i+1] = seq[i+2]
Steps :
1. Take comma separated input from user . This can be done using split(' , ') function which change the input into list named 'seq'.
2.FOR LOOP :Now traverse in above created list using for loop .
NOTE: Range of for loop is from i=0 to i = (len(seq)- 2 )- 1 . It is because for 1st and 2nd element we will not check any condition hence subtract 2 . Also note that : range( n) mean from 0 to (n-1) hence subtract 1 from len(seq)
E.g : seq[ ] = 1 , 2 , 3 , 5 , 8 ; len(seq) = 5
for loop 1st time : i=0 : seq[0] + seq[ 0 + 1 ] = seq[ 0 + 2] => 1 + 2 = 3, which is true ,
for loop 1st time : i=1 : seq[1] + seq[ 1 + 1 ] = seq[ 1 + 2] => 2 + 3 = 5, which is true ,
for loop 1st time : i =2: seq[2] + seq[ 2 + 1 ] = seq[ 2 + 2] => 3 + 5 = 8 which is true .
from here also you can see that range of i is from i=0 to i = len(seq) -2 -1 , i.e i = 2 .
3.Inside for loop we are first typecasting each elements using : int(seq[ i ]) . And then check the fibonacci condition using IF Condition . Typecasting is done because split( ) store elements in form of string in list .
4.If the condition met then increment the value of count .
5. Logic : If the series will be true fibonacci then the IF block runs upto '(length of seq) - 2 ' time and hence the value of count will also be same as the (length of seq) - 2 . i.e count = (length of seq) - 2 .
CODE :
seq =input("Enter the Sequence:").split(',') #taking input from user and converting it to a list using split(',')
count=0
for i in range(len(seq)-2): #for loop
runs from i=0 to (i= length of list -2 -1)
if int(seq[i])+int(seq[i+1])== int(seq[i+2]):
#checking if the sum of 1st two numbers is equal to 3rd or
not
count=count+1 #increment c if condition
met
if count==(len(seq)-2): #for sequence
to be a fibinocci value of count will be (len(seq)-2)
print("Sequence is Fibonacci-Like")
else:
print("Sequence is Not Fibonacci-Like")
SCREENSHOT :
INPUT : 10, 14, 24, 38, 62, 100, 162, 262
OUTPUT: Sequence is Fibonacci - Like