Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '21,': 0.07; 'cache': 0.07; 'subject:missing': 0.07; '34,': 0.09; '[0,': 0.09; 'augmented': 0.09; 'caching,': 0.09; 'def': 0.12; "'''": 0.16; '23,': 0.16; '55,': 0.16; '89,': 0.16; 'arg': 0.16; 'calculates': 0.16; 'iteration': 0.16; 'mutable': 0.16; 'reedy': 0.16; 'seconds.': 0.16; 'true:': 0.16; 'subject: ?': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'space.': 0.24; 'mon,': 0.24; 'skip:_ 20': 0.27; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'needed.': 0.30; 'message-id:@mail.gmail.com': 0.30; '13,': 0.31; 'assert': 0.31; 'initialized': 0.31; 'subject:what': 0.31; 'skip:_ 10': 0.34; 'received:google.com': 0.35; 'should': 0.36; 'to:addr :python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'mar': 0.68; 'default': 0.69; 'us,': 0.73; '2015': 0.84; 'faster.': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=Y628k4uPd+cHF1/cdzAiajKWw+Rqoso86nnYe3rqBJo=; b=zK/LcHS9UlsE/1wOZFY19uv/1qv5U0O88vEalUxxvKa37INZxoixN6XYCNj06rNOcF K24g37fi/jIh+P6uKRxFiogeq/gCpIq78/IG1jdoSp7yzlWhQnzxU8qdbQTPTBSzk+A+ 7HVWFRc6nncoJbTyOWwODuCzvaMGeS/n1mcEFhrwKmsF+ivfeHtTqQR01n70WJa34HtU rZEkFY9ckxFYN/q+P5xopoMKo2tPlwEJaCuyl97wDFj/hs4whWv81thkQBd590ixSIn/ 7rZix6CwbPRWcgBjx2w2xgn28gcSLKj6J7P7DY8kc02gNbAY4DriqdU2xbXIqAJVQcMT OLDQ== X-Received: by 10.66.158.66 with SMTP id ws2mr4356107pab.37.1427174449362; Mon, 23 Mar 2015 22:20:49 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: <55105EF4.2070805@davea.name> From: Ian Kelly Date: Mon, 23 Mar 2015 23:20:08 -0600 Subject: Re: fibonacci series what Iam is missing ? To: Python Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.19 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 40 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1427174452 news.xs4all.nl 2829 [2001:888:2000:d::a6]:55509 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:87866 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]): > '''Return fibonacci(n). > > _cache is initialized with base values and augmented as needed. > ''' > for k in range(len(_cache), n+1): > _cache.append(_cache[k-2] + _cache[k-1]) > return _cache[n] > > print(fib(1), fib(3), fib(6), fib(5)) > # 1 2 8 5 Iteration in log space. On my desktop, this calculates fib(1000) in about 9 us, fib(100000) in about 5 ms, and fib(10000000) in about 7 seconds. def fib(n): assert n >= 0 if n == 0: return 0 a = b = x = 1 c = y = 0 n -= 1 while True: n, r = divmod(n, 2) if r == 1: x, y = x*a + y*b, x*b + y*c if n == 0: return x b, c = a*b + b*c, b*b + c*c a = b + c >>> list(map(fib, range(15))) [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]