-
Notifications
You must be signed in to change notification settings - Fork 0
/
fibonacci.py
41 lines (33 loc) · 924 Bytes
/
fibonacci.py
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
#shortest code in min line
# method 1
def fibonacci(length, first=0, second=1):
if length <= 0:
print("Please enter a positive integer")
elif length == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
else:
for _ in range(length):
print (first)
nth_num = first + second
first = second
second = nth_num
fibonacci(10)
#O/P 0 1 1 2 3 5 8 13 21 34
# method 1
# if you not want to write fun you want to give number as a input the do this type
length = int(input("How many terms? "))
first=0
second=1
if length <= 0:
print("Please enter a positive integer")
elif length == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
else:
for _ in range(length):
print (first)
nth_num = first + second
first = second
second = nth_num
#O/P 0 1 1 2 3 5 8 13 21 34