Fibonacci sequence is defined with an integer n as,
F(n)=F(n-1)+F(n-2) for n ≥2,where F(0)=0,F(1)=1.
develop a Python program to compute F(n) for a given integer between 0 and 20.
Input: Output:
0≤n≤20 F(n)≤2 to the power32
Compute F(n) starting with n=2 and increment iteratively until the required value is obtained.
instructions-
Create a text file include the value ofn. The text file needs to saved in same folder
where the source file has been saved. The file name will be given as input through terminal.
Develop a function named“getNum"to read number n from file.
Develop a function named “show” to display the given value n and computed value
of F(n) on screen. (Expected output format Fibonacci(3) = 2)
Develop a function “saveFile” to write what was displayed in (3) above to a text
file named"result.txt”. This file should be in same folder where source file is.
program must call suitable functions to do the task.
if n>20 print invalid
import shutil
def getfibonacci(n):
f = [0, 1]
for i in range(2, n+1):
f.append(f[i-1] + f[i-2])
return f[n]
print(getfibonacci(12))
src = input("Enter src filename:")
def getNum():
N =N.getfibonacci()
def show():
print(getfibonacci(12))
n=12
print(n)
show()
src = "saveFile"
dest = "result.txt"
shutil.copyfile(src, dest)
Comments
Leave a comment