I had originally coded the program wrongly. Instead of returning the Fibonacci numbers between a range (ie. startNumber 1, endNumber 20 should = only those numbers between 1
How about this one? I guess it's not as fancy as the other suggestions because it demands the initial specification of the previous result to produce the expected output, but I feel is a very readable option, i.e., all it does is to provide the result and the previous result to the recursion.
#count the number of recursions
num_rec = 0
def fibonacci(num, prev, num_rec, cycles):
num_rec = num_rec + 1
if num == 0 and prev == 0:
result = 0;
num = 1;
else:
result = num + prev
print(result)
if num_rec == cycles:
print("done")
else:
fibonacci(result, num, num_rec, cycles)
#Run the fibonacci function 10 times
fibonacci(0, 0, num_rec, 10)
Here's the output:
0
1
1
2
3
5
8
13
21
34
done