I am interested in an iterative algorithm for Fibonacci numbers, so I found the formula on wiki...it looks straight forward so I tried it in Python...it doesn\'t have a prob
The problem is that your return y
is within the loop of your function. So after the first iteration, it will already stop and return the first value: 1. Except when n
is 0, in which case the function is made to return 0
itself, and in case n
is 1, when the for loop will not iterate even once, and no return
is being execute (hence the None
return value).
To fix this, just move the return y
outside of the loop.
Following KebertX’s example, here is a solution I would personally make in Python. Of course, if you were to process many Fibonacci values, you might even want to combine those two solutions and create a cache for the numbers.
def f(n):
a, b = 0, 1
for i in range(0, n):
a, b = b, a + b
return a
def fibiter(n):
f1=1
f2=1
tmp=int()
for i in range(1,int(n)-1):
tmp = f1+f2
f1=f2
f2=tmp
return f2
or with parallel assignment:
def fibiter(n):
f1=1
f2=1
for i in range(1,int(n)-1):
f1,f2=f2,f1+f2
return f2
print fibiter(4)
Possible solution:
a=0
b=1
d=[a,b]
n=int(input("Enter a number"))
i=0
while len(d)<n:
temp=a+b
d.append(temp)
a=temp
b=d[i+1]
i+=1
print("Fibonacci series of {} is {}".format(n,d))
On fib(0), you're returning 0 because:
if (n == 0) {
return 0;
}
On fib(1), you're returning 1 because:
y = 1
return y
On fig(2), you're returning 1 because:
y = 1
return y
...and so on. As long as return y
is inside your loop, the function is ending on the first iteration of your for loop every time.
Here's a good solution that another user came up with: How to write the Fibonacci Sequence in Python
fcount = 0 #a count recording the number of Fibonacci numbers generated
prev = 0
current = 0
next = 1
ll = 0 #lower limit
ul = 999 #upper limit
while ul < 100000:
print("The following Fibonacci numbers make up the chunk between %d and %d." % (ll, ul))
while next <= ul:
print(next)
prev = current
current = next
next = prev + current
fcount += 1 #increments count
print("Number of Fibonacci numbers between %d and %d is %d. \n" % (ll, ul, fcount))
ll = ul + 1 #current upper limit, plus 1, becomes new lower limit
ul += 1000 #add 1000 for the new upper limit
fcount = 0 #set count to zero for a new batch of 1000 numbers
import time
a,b=0,1
def fibton(n):
if n==1:
time.clock()
return 0,time.clock()
elif n==2:
time.clock()
return 1,time.clock()
elif n%2==0:
read="b"
elif n%2==1:
read="a"
else:
time.clock()
for i in range(1,int(n/2)):
a,b=a+b,a+b
if read=="b":
return b,time.clock()
elif read=="a":
return.a,time.clock()
Disclaimer: I am currently on a mobile device and this may not be totally correct
This algorithm utilizes a gap in some other peoples' and now it is literally twice as fast. Instead of just setting b
equal to a
or vice versa and then setting a
to a+b
, I do it twice with only 2 more characters. I also added speed testing, based off of how my other iterative algorithm went. This should be able to go to about the 200,000th Fibonacci number in a second. It also returns the length of the number instead of the whole number, which would take forever.
My other one could go to the second Fibonacci number, as indicated by the built in clock: in 10^-6 seconds. This one can do it in about 5^-6. I'm going to get some more advanced algorithms soon and refine them for utmost speed.