File tree Expand file tree Collapse file tree 1 file changed +7
-22
lines changed Expand file tree Collapse file tree 1 file changed +7
-22
lines changed Original file line number Diff line number Diff line change @@ -8,32 +8,17 @@ def fibonacci(number):
8
8
else :
9
9
return (fibonacci (number - 1 ) + fibonacci (number - 2 ))
10
10
11
- def fibonacciFor (number ):
12
- '''This function calculates the fibonacci series for n-th term using loop'''
13
- # first two terms
14
- n1 = 0
15
- n2 = 1
16
- count = 2
17
- if number <= 0 :
18
- print ("Please enter a positive integer" )
19
- elif number == 1 :
20
- print ("Fibonacci sequence upto " ,number ,":" )
21
- print (n1 )
22
- else :
23
- print ("Fibonacci sequence upto " ,number ,":" )
24
- print (n1 ,n2 ,end = ' ' )
25
- while count <= number :
26
- nth = n1 + n2
27
- print (nth ,end = ' ' )
28
- # update values
29
- n1 = n2
30
- n2 = nth
31
- count += 1
11
+ def fibonacci_without_recursion (number ):
12
+ if number == 0 : return 0
13
+ fibonacci0 , fibonacci1 = 0 , 1
14
+ for i in range (2 , number + 1 ):
15
+ fibonacci1 , fibonacci0 = fibonacci0 + fibonacci1 , fibonacci1
16
+ return fibonacci1
32
17
33
18
if __name__ == '__main__' :
34
19
userInput = int (input ('Enter the number upto which you wish to calculate fibonnaci series: ' ))
35
20
for i in range (userInput + 1 ):
36
21
print (fibonacci (i ),end = ' ' )
37
22
38
23
print ("\n Using LOOP:" )
39
- fibonacciFor ( userInput )
24
+ print ( fibonacci_without_recursion ( userInput ) )
You can’t perform that action at this time.
0 commit comments