@@@@@@@@@@@@@@@@@@@
def fibo(n):
if n <= 1:
return n
else: #2
return(fibo(n-1) + fibo(n-2))
nterms = 100
if nterms <= 0:
print("Plese enter a positive integer")
else:
print("Fibonacci sequence:")
for i in range(nterms):
#print(" I =",i)
print(i,fibo(i))
----------------------------------------
Recursion is the process of defining something in terms of itself.
A physical world example would be to place two parallel mirrors facing each other. Any object in between them would be reflected recursively.
# An example of a recursive function to
# find the factorial of a number
def fact(x):
if x == 1 or x==0:
return 1
else:
return (x * fact(x-1))
n = 3
print("The factorial of", n, "is", fact(n))
-----------------
# Python program to display the Fibonacci sequence up to n-th term using recursive functions
def fibo(n):
if n <= 1:
return n
else: #3
return(fibo(n-1) + fibo(n-2))
nterms = 5
if nterms <= 0:
print("Plese enter a positive integer")
else:
print("Fibonacci sequence:")
for i in range(nterms):
#print(" I =",i)
print(fibo(i))
No comments:
Post a Comment