I have the below code to generate fibonacci series upto 'num'
def fibo(num,a=0,b=1):
while b <= num:
print(b)
a, b = b, a+b
fibo(num,a,b)
fibo(30)
And below is the Output. However if i use 'if' instead of 'while' in my code it works fine. But I wanted to know what is the problem with while here..
1
1
2
3
5
8
13
21
21
13
21
21
8
13
21
21
13
21
21
5
8
13
21
21
13
21
21
8
13
21
21
13
21
21
3
5
8
13
21
21
13
21
21
8
13
21
21
13
21
21
5
8
13
21
21
13
21
21
8
13
21
21
13
21
21
2
3
5
8
13
21
21
13
21
21
8
13
21
21
13
21
21
5
8
13
21
21
13
21
21
8
13
21
21
13
21
21
3
5
8
13
21
21
13
21
21
8
13
21
21
13
21
21
5
8
13
21
21
13
21
21
8
13
21
21
13
21
21
1
2
3
5
8
13
21
21
13
21
21
8
13
21
21
13
21
21
5
8
13
21
21
13
21
21
8
13
21
21
13
21
21
3
5
8
13
21
21
13
21
21
8
13
21
21
13
21
21
5
8
13
21
21
13
21
21
8
13
21
21
13
21
21
2
3
5
8
13
21
21
13
21
21
8
13
21
21
13
21
21
5
8
13
21
21
13
21
21
8
13
21
21
13
21
21
3
5
8
13
21
21
13
21
21
8
13
21
21
13
21
21
5
8
13
21
21
13
21
21
8
13
21
21
13
21
21
You're calling a loop
and also a recursion on fibo
function. You need to use just one, so that this function with if
and recursion
:
def fibo(num,a=0,b=1):
if b <= num:
print(b)
a, b = b, a+b
fibo(num,a,b)
Is equals to this function with while
and without recursion
:
def fibo(num,a=0,b=1):
while b <= num:
print(b)
a, b = b, a+b
Basically if you leave them both you will call the fibo
function at every cycle inside the loop, and every call creates a new cycle in which it will be called again and again.
It's better in Python to use iterative functions instead of recursive. This is because Python DOES NOT optimize tail-recursion
.
You are using recursion on top of your while-loop. Either do it like this:
def fibo(num,a=0,b=1):
if b <= num: #Version with recursion
print(b)
a, b = b, a+b
fibo(num,a,b)
Or like this:
def fibo(num,a=0,b=1): #Version without recursion but with a while-loop
while b <= num:
print(b)
a, b = b, a+b
This is because you are doing call by value. Just remove the recursive function call and try running again
2 possibles implementations are:
def fibo(num,a=0,b=1):
while b <= num:
print(b)
a, b = b, a+b
fibo(30)
or:
def fibo(num,a=0,b=1):
if b <= num:
print(b)
a, b = b, a+b
fibo(num,a,b)
fibo(30)
In the second one, you use recursivity with a breaking condition to stop the iteration, thus the if condition
to stop the program. In the first one, the program loops until it reached the condition. There is no need for recursivity (calling fibo again).
def fibo(num,a=0,b=1):
while b <= num:
print(b)
a, b = b, a+b
fibo(30)
because you are using recursive function and calling function again and again
You mix recursive and iterative approach in the code, so either try a version with iteration and no recursion:
def fibo(num,a=0,b=1):
while b <= num:
print(b)
a, b = b, a+b
fibo(30)
or use recursion and no iteration (ie execute the assignment only once, not in a loop):
def fibo(num,a=0,b=1):
if b <= num:
print(b)
a, b = b, a+b
fibo(num,a,b)
fibo(30)
Try this..
b = 1
a = 0
def fibo(num):
global a, b
while b <= num:
print(b)
a, b = b, a+b
fibo(num)
fibo(30)
This will give the output as below.
1
1
2
3
5
8
13
21
Is this the expected output?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.