Re: fibonacci series what Iam is missing ?

2015-03-24 Thread Ganesh Pal
On Tue, Mar 24, 2015 at 5:41 AM, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: Python does not automatically print all return statements. If you want it to print the intermediate values produced, you will need to add print before each return: py def fib(n): ... if n == 0:

Re: fibonacci series what Iam is missing ?

2015-03-24 Thread Ian Kelly
On Tue, Mar 24, 2015 at 12:20 AM, Rustom Mody rustompm...@gmail.com wrote: On Tuesday, March 24, 2015 at 10:51:11 AM UTC+5:30, Ian wrote: Iteration in log space. On my desktop, this calculates fib(1000) in about 9 us, fib(10) in about 5 ms, and fib(1000) in about 7 seconds. def

Re: fibonacci series what Iam is missing ?

2015-03-24 Thread CHIN Dihedral
On Tuesday, March 24, 2015 at 10:24:59 PM UTC+8, Ian wrote: On Tue, Mar 24, 2015 at 12:20 AM, Rustom Mody rustompm...@gmail.com wrote: On Tuesday, March 24, 2015 at 10:51:11 AM UTC+5:30, Ian wrote: Iteration in log space. On my desktop, this calculates fib(1000) in about 9 us, fib(10)

Re: fibonacci series what Iam is missing ?

2015-03-24 Thread Rustom Mody
On Tuesday, March 24, 2015 at 11:50:40 AM UTC+5:30, Rustom Mody wrote: On Tuesday, March 24, 2015 at 10:51:11 AM UTC+5:30, Ian wrote: On Mon, Mar 23, 2015 at 4:53 PM, Terry Reedy wrote: Iteration with caching, using a mutable default arg to keep the cache private and the function

Re: fibonacci series what Iam is missing ?

2015-03-24 Thread Dave Angel
On 03/23/2015 08:27 PM, Chris Angelico wrote: On Tue, Mar 24, 2015 at 11:09 AM, Dave Angel d...@davea.name wrote: Repeat to self: comment first, then write test, then write the code. Reply to self: Maybe next time. Commenting is like dieting. You can always start tomorrow. It reminds me

Re: fibonacci series what Iam is missing ?

2015-03-24 Thread Rustom Mody
On Tuesday, March 24, 2015 at 10:51:11 AM UTC+5:30, Ian wrote: On Mon, Mar 23, 2015 at 4:53 PM, Terry Reedy wrote: Iteration with caching, using a mutable default arg to keep the cache private and the function self-contained. This should be faster. def fib(n, _cache=[0,1]):

Re: fibonacci series what Iam is missing ?

2015-03-24 Thread CHIN Dihedral
On Tuesday, March 24, 2015 at 6:54:20 AM UTC+8, Terry Reedy wrote: On 3/23/2015 2:44 PM, Dave Angel wrote: ## Example 2: Using recursion with caching cache = [0, 1] def fib4(n): if len(cache) = n: value = fib4(n-2) + fib4(n-1) cache.append(value) return

Re: fibonacci series what Iam is missing ?

2015-03-24 Thread Gregory Ewing
Chris Angelico wrote: Commenting is like dieting. You can always start tomorrow. I've been meaning to become a procrastinator, but I think I'll start tomorrow. In the meantime, since we seem to be having a fibbing competition, here's my contribution, that uses neither recursion nor (explicit)

Re: fibonacci series what Iam is missing ?

2015-03-23 Thread Ian Kelly
On Mon, Mar 23, 2015 at 4:53 PM, Terry Reedy tjre...@udel.edu wrote: Iteration with caching, using a mutable default arg to keep the cache private and the function self-contained. This should be faster. def fib(n, _cache=[0,1]): '''Return fibonacci(n). _cache is initialized with

Re: fibonacci series what Iam is missing ?

2015-03-23 Thread Chris Angelico
On Tue, Mar 24, 2015 at 3:01 AM, Ganesh Pal ganesh1...@gmail.com wrote: Hello team , [root@localhost Python]# cat fibonacci-Sequence-3.py ## Example 2: Using recursion def fib(n): if n == 0: return 0 elif n == 1: return 1 else: return fib(n-1) +

fibonacci series what Iam is missing ?

2015-03-23 Thread Ganesh Pal
Hello team , [root@localhost Python]# cat fibonacci-Sequence-3.py ## Example 2: Using recursion def fib(n): if n == 0: return 0 elif n == 1: return 1 else: return fib(n-1) + fib(n-2) print fib(5) # python fibonacci-Sequence-3.py 5 what Iam I missing in

Re: fibonacci series what Iam is missing ?

2015-03-23 Thread Dave Angel
On 03/23/2015 12:01 PM, Ganesh Pal wrote: Hello team , [root@localhost Python]# cat fibonacci-Sequence-3.py ## Example 2: Using recursion def fib(n): if n == 0: return 0 elif n == 1: return 1 else: return fib(n-1) + fib(n-2) print fib(5) # python

Re: fibonacci series what Iam is missing ?

2015-03-23 Thread Ian Kelly
On Mon, Mar 23, 2015 at 12:44 PM, Dave Angel da...@davea.name wrote: On 03/23/2015 12:16 PM, Chris Angelico wrote: [1] Cue the demonstration of a worse version from someone's student? I'll give you a worse version. Back in the day I had occasion to write a simple program in a language which

Re: fibonacci series what Iam is missing ?

2015-03-23 Thread Carsten Lechte
On 23/03/15 19:44, Dave Angel wrote: I'll give you a worse version. Back in the day I had occasion to write a simple program in a language which had no add or subtract. It could only increment and decrement indices. Oh yes, the olden days. I have not quite lived through the punch card era,

Re: fibonacci series what Iam is missing ?

2015-03-23 Thread Chris Angelico
On Tue, Mar 24, 2015 at 3:16 AM, Dave Angel da...@davea.name wrote: An entirely separate question is whether you can gain performance by caching intermediate values. For example, if you capture values in a list, you could potentially save a lot of time, at least for non-trivial values of n.

Re: fibonacci series what Iam is missing ?

2015-03-23 Thread CHIN Dihedral
On Tuesday, March 24, 2015 at 1:00:11 AM UTC+8, Chris Angelico wrote: On Tue, Mar 24, 2015 at 3:16 AM, Dave Angel da...@davea.name wrote: An entirely separate question is whether you can gain performance by caching intermediate values. For example, if you capture values in a list, you

Re: fibonacci series what Iam is missing ?

2015-03-23 Thread Dave Angel
On 03/23/2015 12:16 PM, Chris Angelico wrote: On Tue, Mar 24, 2015 at 3:01 AM, Ganesh Pal ganesh1...@gmail.com wrote: Hello team , [root@localhost Python]# cat fibonacci-Sequence-3.py ## Example 2: Using recursion def fib(n): if n == 0: return 0 elif n == 1:

Re: fibonacci series what Iam is missing ?

2015-03-23 Thread Dave Angel
On 03/23/2015 12:59 PM, Chris Angelico wrote: On Tue, Mar 24, 2015 at 3:16 AM, Dave Angel da...@davea.name wrote: An entirely separate question is whether you can gain performance by caching intermediate values. For example, if you capture values in a list, you could potentially save a lot of

Re: fibonacci series what Iam is missing ?

2015-03-23 Thread Steven D'Aprano
On Tue, 24 Mar 2015 03:16 am, Chris Angelico wrote about the standard recursive version of the Fibonacci series: On Tue, Mar 24, 2015 at 3:01 AM, Ganesh Pal ganesh1...@gmail.com wrote: def fib(n): if n == 0: return 0 elif n == 1: return 1 else: return

Re: fibonacci series what Iam is missing ?

2015-03-23 Thread Dave Angel
On 03/23/2015 05:31 PM, Carsten Lechte wrote: On 23/03/15 19:44, Dave Angel wrote: I'll give you a worse version. Back in the day I had occasion to write a simple program in a language which had no add or subtract. It could only increment and decrement indices. Oh yes, the olden days. I

Re: fibonacci series what Iam is missing ?

2015-03-23 Thread Dave Angel
On 03/23/2015 07:49 PM, Chris Angelico wrote: On Tue, Mar 24, 2015 at 10:12 AM, Dave Angel da...@davea.name wrote: On the other hand, my loop makes some non-obvious assumptions, like that append is always the right place to put a new value. Yes, I had to stop and think about that one a bit.

Re: fibonacci series what Iam is missing ?

2015-03-23 Thread Chris Angelico
On Tue, Mar 24, 2015 at 10:12 AM, Dave Angel da...@davea.name wrote: On the other hand, my loop makes some non-obvious assumptions, like that append is always the right place to put a new value. Yes, I had to stop and think about that one a bit. And that strongly suggests (to me, at least!)

Re: fibonacci series what Iam is missing ?

2015-03-23 Thread Chris Angelico
On Tue, Mar 24, 2015 at 11:19 AM, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: It is pretty inefficient, but it is a good toy example of recursion. It's also a good example of how *not* to write the Fibonacci series in practice, what is mathematically straightforward is not

Re: fibonacci series what Iam is missing ?

2015-03-23 Thread Dave Angel
On 03/23/2015 06:53 PM, Terry Reedy wrote: On 3/23/2015 2:44 PM, Dave Angel wrote: ## Example 2: Using recursion with caching cache = [0, 1] def fib4(n): if len(cache) = n: value = fib4(n-2) + fib4(n-1) cache.append(value) return cache[n] This one takes less than a

Re: fibonacci series what Iam is missing ?

2015-03-23 Thread Steven D'Aprano
On Tue, 24 Mar 2015 03:01 am, Ganesh Pal wrote: Hello team , [root@localhost Python]# cat fibonacci-Sequence-3.py ## Example 2: Using recursion def fib(n): if n == 0: return 0 elif n == 1: return 1 else: return fib(n-1) + fib(n-2) print fib(5)

Re: fibonacci series what Iam is missing ?

2015-03-23 Thread Chris Angelico
On Tue, Mar 24, 2015 at 11:09 AM, Dave Angel d...@davea.name wrote: Repeat to self: comment first, then write test, then write the code. Reply to self: Maybe next time. Commenting is like dieting. You can always start tomorrow. ChrisA -- https://mail.python.org/mailman/listinfo/python-list

Re: fibonacci series what Iam is missing ?

2015-03-23 Thread Steven D'Aprano
Given the standard recursive version of the Fibonacci series: def fib(n): if n == 0: return 0 elif n == 1: return 1 else: return fib(n-1) + fib(n-2) I asked: The question is, just how inefficient is is? How many calls to `fib` are made in calling fib(n)?

Re: fibonacci series what Iam is missing ?

2015-03-23 Thread Terry Reedy
On 3/23/2015 2:44 PM, Dave Angel wrote: ## Example 2: Using recursion with caching cache = [0, 1] def fib4(n): if len(cache) = n: value = fib4(n-2) + fib4(n-1) cache.append(value) return cache[n] This one takes less than a millisecond up to n=200 or so. Iteration

Re: fibonacci series what Iam is missing ?

2015-03-23 Thread Dave Angel
On 03/23/2015 08:19 PM, Steven D'Aprano wrote: On Tue, 24 Mar 2015 03:16 am, Chris Angelico wrote about the standard recursive version of the Fibonacci series: On Tue, Mar 24, 2015 at 3:01 AM, Ganesh Pal ganesh1...@gmail.com wrote: def fib(n): if n == 0: return 0 elif n

Re: fibonacci series what Iam is missing ?

2015-03-23 Thread Terry Reedy
On 3/23/2015 7:12 PM, Dave Angel wrote: On 03/23/2015 06:53 PM, Terry Reedy wrote: Iteration with caching, using a mutable default arg to keep the cache private and the function self-contained. This should be faster. def fib(n, _cache=[0,1]): '''Return fibonacci(n). _cache is

Re: fibonacci series what Iam is missing ?

2015-03-23 Thread Steven D'Aprano
On Tue, 24 Mar 2015 11:59 am, Chris Angelico wrote: I've never thought of the mathematical definition as being inherently recursive; but as inherently sequential. Sure, you can define counting numbers based on sets (0 is the cardinality of the empty set, 1 is the cardinality of the set

Re: fibonacci series what Iam is missing ?

2015-03-23 Thread Rustom Mody
On Tuesday, March 24, 2015 at 8:33:24 AM UTC+5:30, Chris Angelico wrote: On Tue, Mar 24, 2015 at 12:52 PM, Steven D'Aprano wrote: On Tue, 24 Mar 2015 11:59 am, Chris Angelico wrote: I've never thought of the mathematical definition as being inherently recursive; but as inherently

Re: fibonacci series what Iam is missing ?

2015-03-23 Thread Rustom Mody
On Tuesday, March 24, 2015 at 8:33:24 AM UTC+5:30, Chris Angelico wrote: Mathematics doesn't like defining sequences, except by defining functions, and so it has to convert the concept of defining the Fibonacci sequence into defining a function F(N) which returns the Nth Fibonacci number, and

Re: fibonacci series what Iam is missing ?

2015-03-23 Thread Rustom Mody
On Tuesday, March 24, 2015 at 7:22:25 AM UTC+5:30, Steven D'Aprano wrote: On Tue, 24 Mar 2015 11:59 am, Chris Angelico wrote: I've never thought of the mathematical definition as being inherently recursive; but as inherently sequential. Sure, you can define counting numbers based on

Re: fibonacci series what Iam is missing ?

2015-03-23 Thread Chris Angelico
On Tue, Mar 24, 2015 at 12:52 PM, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: On Tue, 24 Mar 2015 11:59 am, Chris Angelico wrote: I've never thought of the mathematical definition as being inherently recursive; but as inherently sequential. Sure, you can define counting