In: Computer Science
write an algorithm program using python or C++ where a0=1, a1=2 an=an-1*an-2, find an ,also a5=?
# Python program to create a function that calculates the value of an at n
# given a0 = 1 and a1 = 2 and an = an-1 * an-2 , n > 1
# function to calculate the value of an at n
def find_an(n):
if n == 0: # if n=0 return 1
return 1
elif n == 1: # if n=1 return 2
return 2
else: # return the product of function call with n-1 and n-2
return find_an(n-1)*find_an(n-2)
# call and print the value of an at n=5
print(find_an(5))
#end of program
Code Screenshot:
Output: