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...
A Fibonacci sequence, is a sequence of numbers with the property that each number is the...
A Fibonacci sequence, is a sequence of numbers with the property that each number is the sum of the two preceding Fibonacci numbers, starting from 0 and 1. Fibonacci number are usually denoted by Fn, where Fn is the nth Fibonacci number. Thus Fn = 0, and Fn = 1, and Fn = Fn-1 + Fn-2, n ≥ 2. Here are the first few Fibonacci numbers: F0=0 (by definition) F1=1 (by definition) F2 = F1 + F0 = 1 +...
( 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...
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"""...
Using Python: The Fibonacci sequence is a famous series of numbers with the following rules: The...
Using Python: The Fibonacci sequence is a famous series of numbers with the following rules: The first number in the sequence is 0 - The second number in the sequence is 1 - The other numbers in the sequence are composed by adding up the two previous numbers in the sequence. We therefore have the following sequence: 1 st number: 0 2nd number: 1 3 rd number: 0 + 1 = 1 4 th number: 1+1 =2 5 th number:...
Use Python to solve, show all code Write a program that sums a sequence of integers...
Use Python to solve, show all code Write a program that sums a sequence of integers from a starting integer to and ending integer. The sequence is generated by skipping a value (e.g. 1,3,5,… or 2,6,10,14…). Prompt the user for a) a starting value greater than 0. Validate the value is greater than 0. b) the ending value. c) the value to skip Print out the sum of the numbers for the sequence. If the starting value is 2, the...
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...
Python program please no def, main, functions Given a list of negative integers, write a Python...
Python program please no def, main, functions Given a list of negative integers, write a Python program to display each integer in the list that is evenly divisible by either 5 or 7. Also, print how many of those integers were found. Sample input/output: Enter a negative integer (0 or positive to end): 5 Number of integers evenly divisible by either 5 or 7: 0 Sample input/output: Enter a negative integer (0 or positive to end): -5 -5 is evenly...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT