hi all
im trying to create a function that identifies if a number is a Fibonacci numbers base on:
Fibonacci number - Wikipedia, the free encyclopedia
here is what i did
and hear id what i gotCode:import math gratio = (1 + math.sqrt(5))/2#golden ratio lim = 10**999 #math.log(x[, base]) def isFibon(x):#test if number is a Fibonacci numbers if (math.log(math.sqrt(5*x),gratio) + 0.5) == x: return 1 else: return 0 for n in range(20): if isFibon(n) == 1: print n
Traceback (most recent call last):
File "untitled.py", line 15, in <module>
if isFibon(n) == 1:
File "untitled.py", line 9, in isFibon
if (math.log(math.sqrt(5*x),gratio) + 0.5) == x:
OverflowError: math range error
im planning on using another method but im still curious what all this mean
any help will be appreciated
I'm guessing that means that your function generated a number way too big for python to support. Honestly I'm not sure, but I believe that is what that means.
How about a recursive method?
?Code:function fib(int n) { if (n == 1) return 1; return fib(n-1) + fib(n-2); }
that sounds like it, i guess i forgot even python has a limit with how big its values can be.
thanks the recursive method would work but i had already resorted to a simple loop method
Thanks
You're welcome!![]()
google "how to think like a computer scientist, learning with python". Its a free set of tutorials to python.
it first introduces this problem very early on, then comes back to it much later in the chapter on lists I think.
thanks ill look it up
You could use Decimals instead. They can deal with, theoretically, any finite values and by default work to about 18 significant figures. They are slower than floats though...
I can't paste the link to the Python docs on Decimals since I haven't made 10 posts yet, but Googling for "python decimal" should get you close.
Yes, the second link was what I had in mind.![]()
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks