Monday, 13 December 2021

Develop a python program to calculate the next number in the Fibonacci series for a given number (which may or may not be in the Fibonacci series).

 The Fibonacci numbers are the numbers in the following integer sequence.
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ……..

In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation 

Fn = Fn-1 + Fn-2

with fixed values

F0 = 0 and F1 = 1.

The following example is an advanced implementation which combines Fibonacci series generation and searching next number for Fibonacci number which may or may not be in the Fibonacci series.

def fib_rec(n):
    if n == 1:
        return [0]
    elif n == 2:
        return [0,1]
    else:
        x = fib_rec(n-1)
        # the new element the sum of the last two elements
        x.append(sum(x[:-3:-1]))
        return x
x=int(input("Enter the number of terms "))
result=fib_rec(x)
print(result)
n=int(input("enter any number from the above series"))

c=0
a=1
b=1
if n==0 or n==1:
    print("Yes")
else:
    while c<n:
        c=a+b
        b=a
        a=c

if c==n:
 Next_number=n+1;
 print("next number is ",Next_number)
else:
        print("Enter correct number")


The output of the program is 

Rune-1: On correct input the out put is

Enter the number of terms 10
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
enter any number from the above series13
next number is  14

As I supplied number from series it will generate next number to that number that is not present in the list.

Rune-1: On Wrong input the out put is

 Enter the number of terms 10
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
enter any number from the above series15

Enter correct number

0 comments :

Post a Comment

Note: only a member of this blog may post a comment.

Machine Learning

More

Advertisement

Java Tutorial

More

UGC NET CS TUTORIAL

MFCS
COA
PL-CG
DBMS
OPERATING SYSTEM
SOFTWARE ENG
DSA
TOC-CD
ARTIFICIAL INT

C Programming

More

Python Tutorial

More

Data Structures

More

computer Organization

More
Top