-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfib.py
More file actions
executable file
·51 lines (43 loc) · 975 Bytes
/
fib.py
File metadata and controls
executable file
·51 lines (43 loc) · 975 Bytes
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
def fib(n):
a,b=0,1
if n ==0:
print("Enter the value greater than 0",a)
elif n ==1:
print(a , b, sep="\n")
else:
print(a,b, sep="\n")
for i in range(2,n):
c= a+b
a=b
b=c
#for upto 100 nums fib
# if c<=100:
# break
print(c)
n = int(input("Enter the Num for fib "))
fib(n)
#2. Recursive Method
def recursive_fib(n):
if n <=0:
return 0
elif n == 1:
return 1
else:
return recursive_fib(n-1)+ recursive_fib(n-2)
def print_fib(n):
for i in range(n+1):
print(f"fib at {i} is {recursive_fib(i)}")
n = int(input("Enter the num "))
#recursive_fib(n)
print_fib(n)
# iterative Method
def iterative_fib(n):
a,b=0,1
print(a)
for _ in range(n):
a,b = b, a+b
print(a)
return a
n = int(input("Enter the Num for fib "))
iterative_fib(n)
#print(iterative_fib(n))