Question

In: Computer Science

PLEASE WRITE IN PYTHON A sequence of integers is said to be Fibonacci-like if each element...

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.’’

Solutions

Expert Solution

********************************************** 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


Related Solutions

The Fibonacci Sequence is a series of integers. The first two numbers in the sequence are...
The Fibonacci Sequence is a series of integers. The first two numbers in the sequence are both 1; after that, each number is the sum of the preceding two numbers. 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... For example, 1+1=2, 1+2=3, 2+3=5, 3+5=8, etc. The nth Fibonacci number is the nth number in this sequence, so for example fibonacci(1)=1, fibonacci(2)=1, fibonacci(3)=2, fibonacci(4)=3, etc. Do not use zero-based counting; fibonacci(4)is 3, not 5. Your assignment...
In Python Write a Fibonacci class to calculate next number in the 'Fibonacci' class by the...
In Python Write a Fibonacci class to calculate next number in the 'Fibonacci' class by the 'nxt' method. In this class, the 'val' member is a Fibonacci number. The 'nxt' method will return a 'Fibonacci' object whose value is the next number in Fibonacci series. class Fibonacci (): """A Fibonacci number. >>>a = Fibonacci(): >>>a 0 >>>a.nxt() 1 >>>a.nxt().nxt() 1 >>>a.nxt().nxt().nxt() 2 >>>a.nxt().nxt().nxt().nxt() 3 >>>a.nxt().nxt().nxt().nxt().nxt() 5 >>>a.nxt.nxt().nxt().nxt().nxt().nxt() 8 """ def __init__(self): self.val = 0 def nxt(self): """YOUR SOURCE CODE HERE"""...
( Assembly Language ) Write a program that computes the 7th fibonacci number. The fibonacci sequence...
( Assembly Language ) Write a program that computes the 7th fibonacci number. The fibonacci sequence - 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, … what is the initialization of a, b, and d? - a b d 1 ? ? 1 2 ? ? 1 3 1 1 2 4 1 2 3 5 2 3 5 6 3 5 8 7 5 8 13 wrong initialization - a b d 1 0 1 1 2...
The Fibonacci sequence is the series of integers 0, 1, 1, 2, 3, 5, 8, 13,...
The Fibonacci sequence is the series of integers 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 . . . See the pattern? Each element in the series is the sum of the preceding two elements. Here is a recursive formula for calculating the nth number of the sequence: Fib(N) = {N, if N = 0 or 1 Fib(N - 2) + Fib(N - 1), if N > 1 a) Write a recursive method fibonacci that returns...
Python: Using Jupyter Notebook 1. Write code to generate Fibonacci series. Fibonacci numbers – 1, 1,...
Python: Using Jupyter Notebook 1. Write code to generate Fibonacci series. Fibonacci numbers – 1, 1, 2, 3, 5, 8, … 2. Check if a number is an Armstrong number A positive integer is called an Armstrong number of order n if abcd... = a^n + b^n + c^n + d^n + ... In case of an Armstrong number of 3 digits, the sum of cubes of each digits is equal to the number itself. For example: 153 = 1*1*1...
Program must be in Python Write a program in Python whose inputs are three integers, and...
Program must be in Python Write a program in Python whose inputs are three integers, and whose output is the smallest of the three values. Input is 7 15 3
Python: Write a function that receives a one dimensional array of integers and returns a Python...
Python: Write a function that receives a one dimensional array of integers and returns a Python tuple with two values - minimum and maximum values in the input array. You may assume that the input array will contain only integers and will have at least one element. You do not need to check for those conditions. Restrictions: No built-in Python data structures are allowed (lists, dictionaries etc). OK to use a Python tuple to store and return the result. Below...
Write a c++ program of the Fibonacci Sequence. Have the user enter a positive integer n...
Write a c++ program of the Fibonacci Sequence. Have the user enter a positive integer n and compute the nth Fibonacci number. The program should end when the user enters a number less than or equal to zero
Write Java code to generate 100 random integers ranging from 0..9, inserting each element into an...
Write Java code to generate 100 random integers ranging from 0..9, inserting each element into an ArrayList. Then search for the first instance of the number 3, print the position, and then remove it from the list.
Write a C++ program that inputs a sequence of integers into a vector, computes the average,...
Write a C++ program that inputs a sequence of integers into a vector, computes the average, and then outputs the # of input values, the average, and the values themselves. There are 0 or more inputs values, followed by a negative value as the sentinel; the negative value is not stored and not counted. The following sample input: 10 20 0 99 -1 would produce the following output: N: 4 Avg: 32.25 10 20 0 99 The main program has...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT