Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 20 Online
OpenStudy (anonymous):

http://codepad.org/DoJJJoQz -- I was wondering if someone could help me with this code. First, I want to find a way to list negafibonacci numbers (negative fibonacci numbers) when a user inputs a negative number. I know the algorithm for this, but it always exceeds the recursion limit or gives me a segmentation fault (presumably because the fibonacci algorithm is trying to get to zero and because of the subtraction it will never reach zero inside the sub zero set.) The other thing I'm having a problem with is displaying the golden ratio when I calculate fib(x)//fib(x-1) Help please?

OpenStudy (anonymous):

This part seems to work as long as memo is 'seeded' correctly http://codepad.org/eRFOsZw1 If you want to extend to negative indices, you have to: - test the user input for a negative index - use one formula for positive indices and the other formula for negative indices if index is positive: use positive index formula if index is negative: use negative index formula http://codepad.org/q5L2E1mV

OpenStudy (anonymous):

Thanks it works like a charm. Now, is there anyway to avoid segmentation faults (when I enter fib(100000))?

OpenStudy (anonymous):

I am presuming that the when a number is too large, the computer and python can't cope with it. What about the golden ratio problem?

OpenStudy (anonymous):

what do you mean by 'displaying' the golden ratio?? if you want to draw a graph, matplolib is pretty easy to use and very useful http://en.wikipedia.org/wiki/Golden_ratio#Relationship_to_Fibonacci_sequence

OpenStudy (anonymous):

When I divide fib(x-1) into fib(x), it should give me a close approximation of the golden ratio, right? But in my code, it rounds to one.

OpenStudy (anonymous):

Got it to work. Thanks. Though I can't help but think there is a better way to handle it.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!