-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path23_recursive_vs_iterative.py
More file actions
55 lines (46 loc) · 1.26 KB
/
Copy path23_recursive_vs_iterative.py
File metadata and controls
55 lines (46 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Recursion
# two types of recursion : iterative and recursive
'''Iterative Method for finding factorial'''
# def factorial_iterative(n):
# """
# :param n: Integer
# :return: n * n-1 * n-2 * n-3....1
# """
# fac = 1
# for i in range(n):
# fac = fac * (i+1)
# return fac
#
# number = int(input("Enter the number: "))
# print("Factorial using iterative method:", factorial_iterative(number))
'''Recursive Method for finding factorial'''
# def factorial_recursive(n):
# """
# :param n: Integer
# :return: n * n-1 * n-2 * n-3....1
# """
# if n==1:
# return 1
# else:
# return n * factorial_recursive(n-1) # factorial of three = 3*2*1
# '''
# 3 * factorial_recursive(2)
# 3 * 2 * factorial_recursive(1)
# 3 * 2 * 1
# '''
#
# number = int(input("Enter the number: "))
# print("Factorial using recursive method:", factorial_recursive(number))
'''Recursive Method for find the no. in fibonacci series'''
def fibonacci(n):
"""
:param n: 0, 1, 1, 2, 3, 5...nth
"""
if n==1:
return 0
elif n==2:
return 1
else:
return fibonacci(n-1) + fibonacci(n-2)
number = int(input("Enter the number: "))
print("fibonacci series no. is: ", fibonacci(number))