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

Write a recursive and an iterative function to calculate the nth element in a Fibonacci sequence....
Write a recursive and an iterative function to calculate the nth element in a Fibonacci sequence. A Fibonacci sequence is defined as the element 1, followed by another 1, and each element thereafter is the sum of the previous two elements. For example, the first 9 elements of a Fibonacci sequence are: 1 2 3 5 8 13 21 34 This famous sequence was originally used to predict the growth of rabbit populations! Once you have each of the functions...
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...
One Problem/Question (Four Parts) Write a python program which prints a sequence of integers that are...
One Problem/Question (Four Parts) Write a python program which prints a sequence of integers that are multiples of 10, starting at 0 and ending at 100. Write a python program that computes the average of 10 random odd integers, ranging from 0 to 500. Write a python program that prints whatever the user enters, and stops when the user types “done”, using an infinite loop and a break statement. Same as number three but use a condition. The loop terminates...
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...
Please write the following Python program. Also, show all output work. Computing the Fibonacci and Lucas...
Please write the following Python program. Also, show all output work. Computing the Fibonacci and Lucas Series¶ Goal:¶ The Fibonacci Series is a numeric series starting with the integers 0 and 1. In this series, the next integer is determined by summing the previous two. This gives us: 0, 1, 1, 2, 3, 5, 8, 13, ... We will write a function that computes this series – then generalize it. Step 1¶ Create a new module series.py in the session02...
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...
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
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT