PDA

View Full Version : want to know how to make recursive fibonacci sequence program



baldy1324
October 9th, 2006, 11:39 PM
hi i would like to make a program that does the fibonacci sequence by calling the function (recursion) 1000 times. each times it does this 1 time, the program prints the number so using code like this -

#!/usr/bin/python
def fibiterative():
n = 1000
first = 0
second = 1
third = 0
print first
print " "
print second

while n>0:
third = first+second
first = second
second = third
print third
print " "
n=n-1

fibiterative()

but using recursion instead of while loops

eeGhoong0ais
October 10th, 2006, 12:07 AM
Er ... forgive me if I'm jumping to conclusions here, but this looks very much like a homework assignment.

jpkotta
October 10th, 2006, 12:18 AM
Just look it up in Google or Wikipedia. There are so many examples that it's not even funny.

Note360
October 10th, 2006, 12:29 AM
Usually one uses lisp, haskell, or nemerle for recursion (functional programming). However, the same concept apply to python as far as recursion go.

meng
October 10th, 2006, 04:46 PM
Word of warning: Using python to calculate fibonacci sequence to 1000 by recursion could take a LOT of processor time (think hours) unless you intend to use a dictionary to look-up previously calculated values.

Perhaps you could use this exercise to learn about generators rather than recursion!?

Also, I agree that you should take advantage of websearching to get instant answers to these fairly straightforward questions. Most Python texts (including online ones) cover this as a routine exercise.

Finally, if one of your aims is to litter the forums with several threads on essentially the same topic, then you're doing a great job! :p

skeeterbug
October 10th, 2006, 10:30 PM
I was looking for the ignore button next to his name. Looks like this forum doesn't have it :(

meng
October 10th, 2006, 10:31 PM
Oh be nice! You can set your mental filters if you need to.

skeeterbug
October 10th, 2006, 10:46 PM
Sorry, I took the bait. It's been two or three days now and every day there seems to be one or two fibonacci posts from this guy. I wonder if he has ever been to google.com

:twisted:


OK OK, on a serious note. Check out this url.

http://www.google.com/codesearch/advanced_code_search

It might help more than this forum, who knows!

meng
October 11th, 2006, 12:01 AM
True, some folks need more encouragement to learn how to help themselves.

johnnymac
October 11th, 2006, 01:30 AM
IMHO google'n it is the easy way out. Do it like the rest of us had to before google (hmm - perhaps I show my age) and READ...you'd be amazed at just how much your "I wann learn to program" book will show you about recursion.

Funny though....school says this is how recursion works....industry says...AHHHHH Don't use recursion!!

Note360
October 11th, 2006, 01:45 AM
If you really want to use recursion you wont want to use python. You need a functional programming language. I would suggest Nemerle or LISP. If you want to do functional programming in python you should read up on functional programming first (because python is mainly imperative). Then you can implement functional programming in any language. (You should look into python lambada, and tuples. Also you will need a class for everything). I will admit that I like functional programming alot, but it has its places.