Re: Fibonacci: How to think recursively

2010-09-01 Thread Mike
The most straightforward method would be to apply the formula directly. Loop on j computing Fj along the way if n<=1 : return n Fold=0 Fnew=1 for j in range(2,n) : Fold, Fnew = Fnew, Fold+Fnew return Fnew Even simpler: return round(((1+sqrt(5.))/2)**n/sqrt(5.)) -- http://mail.python.org/mail

Re: Fibonacci: How to think recursively

2010-09-01 Thread Neil Cerutti
On 2010-09-01, Albert van der Horst wrote: > [Didn't you mean: I don't understand what you mean by > overlapping recursions? You're right about the base case, so > clearly the OP uses some confusing terminology.] > > I see a problem with overlapping recursions. Unless automatic > memoizing is one

Re: Fibonacci: How to think recursively

2010-09-01 Thread Albert van der Horst
In article , Mel wrote: >Baba wrote: > >> Level: beginner >> >> I would like to know how to approach the following Fibonacci problem: >> How may rabbits do i have after n months? >> >> I'm not looking for the code as i could Google that very easily. I'm >> looking for a hint to put me on the righ

Re: Fibonacci: How to think recursively

2010-08-29 Thread Steven D'Aprano
On Sun, 29 Aug 2010 06:43:40 -0700, Baba wrote: > So here's my code. It does still cause me one headache. If i use f(0)=0 > and f(1)=1 as base cases the result will be 144. I was expecting the > result to be the next value in the series (233)... That's because you're not generating the Fibonacci

Re: Fibonacci: How to think recursively

2010-08-29 Thread Baba
Thanks to All for your kind help! Baba -- http://mail.python.org/mailman/listinfo/python-list

Re: Fibonacci: How to think recursively

2010-08-29 Thread News123
Hi Baba, > So here's my code. It does still cause me one headache. If i use > f(0)=0 > and f(1)=1 as base cases the result will be 144. I was expecting the > result to be the next value in the series (233)... > If i use f(1)=1 and f(2)=2 as base cases them i get my expected > result. I assume this

Re: Fibonacci: How to think recursively

2010-08-29 Thread Alain Ketterlin
Baba writes: > Level: beginner > > I would like to know how to approach the following Fibonacci problem: > How may rabbits do i have after n months? > > I'm not looking for the code as i could Google that very easily. I'm > looking for a hint to put me on the right track to solve this myself > wi

Re: Fibonacci: How to think recursively

2010-08-29 Thread Baba
On Aug 29, 3:25 am, Steven D'Aprano wrote: > Mathematically, there is nothing wrong with overlapping recursion. It > will work, and Python can handle it easily. Based on the advice by Steven and Mel i tried my initial 'guess' and it does seem to work fine. When looking at it using pencil and pap

Re: Fibonacci: How to think recursively

2010-08-29 Thread News123
On 08/29/2010 01:12 AM, Baba wrote: > Level: beginner > > I would like to know how to approach the following Fibonacci problem: > How may rabbits do i have after n months? > > I'm not looking for the code as i could Google that very easily. I'm > looking for a hint to put me on the right track to

Re: Fibonacci: How to think recursively

2010-08-28 Thread Chris Rebert
On Sat, Aug 28, 2010 at 7:25 PM, Steven D'Aprano wrote: > On Sat, 28 Aug 2010 16:12:36 -0700, Baba wrote: >> Level: beginner >> >> I would like to know how to approach the following Fibonacci problem: >> my attempted rough code: >> >> def fibonacci(n): >>     # base case: >>         result = fibo

Re: Fibonacci: How to think recursively

2010-08-28 Thread Mark Tolonen
"Steven D'Aprano" wrote in message news:4c79c510$0$28655$c3e8...@news.astraweb.com... On Sat, 28 Aug 2010 16:12:36 -0700, Baba wrote: There are other techniques, but this will do to get started. Once you realize recursion for Fibonacci numbers is still fairly slow, look up generator func

Re: Fibonacci: How to think recursively

2010-08-28 Thread Steven D'Aprano
On Sat, 28 Aug 2010 16:12:36 -0700, Baba wrote: > Level: beginner > > I would like to know how to approach the following Fibonacci problem: > How may rabbits do i have after n months? > > I'm not looking for the code as i could Google that very easily. I'm > looking for a hint to put me on the r

Re: Fibonacci: How to think recursively

2010-08-28 Thread Matteo Landi
I suggest you to memoize results in order to prevent overlapping recursion. Regards, Matteo On Sun, Aug 29, 2010 at 2:23 AM, Ben Finney wrote: > Baba writes: > >> my brainstorming so far brought me to a stand still as i can't seem to >> imagine a recursive way to code this: >> >> my attempted r

Re: Fibonacci: How to think recursively

2010-08-28 Thread Ben Finney
Baba writes: > my brainstorming so far brought me to a stand still as i can't seem to > imagine a recursive way to code this: > > my attempted rough code: > > def fibonacci(n): > # base case: > result = fibonacci (n-1) + fibonacci (n-2) > >> this will end up in a mess as it will creat

Re: Fibonacci: How to think recursively

2010-08-28 Thread Mel
Baba wrote: > Level: beginner > > I would like to know how to approach the following Fibonacci problem: > How may rabbits do i have after n months? > > I'm not looking for the code as i could Google that very easily. I'm > looking for a hint to put me on the right track to solve this myself > wi