Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #87885
| Path | csiph.com!usenet.pasdenom.info!news.franciliens.net!fdn.fr!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <ian.g.kelly@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.010 |
| X-Spam-Evidence | '*H*': 0.98; '*S*': 0.00; 'essentially': 0.04; 'assignment': 0.07; 'subject:missing': 0.07; 'linear': 0.09; 'reached.': 0.09; 'def': 0.12; '24,': 0.16; 'above?': 0.16; 'calculates': 0.16; 'evaluates': 0.16; 'i.e.,': 0.16; 'identities': 0.16; 'iteration': 0.16; 'numpy': 0.16; 'seconds.': 0.16; 'true:': 0.16; 'two.': 0.16; 'uses,': 0.16; 'elements': 0.16; 'subject: ?': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'written': 0.21; '(in': 0.22; 'space.': 0.24; 'updating': 0.26; 'header:In- Reply-To:1': 0.27; 'am,': 0.29; "doesn't": 0.30; 'message- id:@mail.gmail.com': 0.30; '(which': 0.31; 'code': 0.31; 'lines': 0.31; 'assert': 0.31; 'equality': 0.31; 'subject:what': 0.31; 'could': 0.34; 'but': 0.35; 'received:google.com': 0.35; 'maintained': 0.36; 'transition': 0.36; 'shows': 0.36; 'two': 0.37; 'expressed': 0.37; 'needed': 0.38; 'to:addr:python-list': 0.38; 'fact': 0.38; 'rather': 0.38; 'little': 0.38; 'to:addr:python.org': 0.39; 'expression': 0.60; 'ian': 0.60; 'new': 0.61; 'march': 0.61; 'mar': 0.68; 'optimized': 0.68; 'results': 0.69; 'containing': 0.69; 'us,': 0.73; 'power': 0.76; 'as:': 0.81; '2015': 0.84; 'multiplying': 0.84; 'fibonacci': 0.91 |
| 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=7pCFCBnlkeRNqVLyIjDjR8p0QALQYlSjsIxocuJI6Zk=; b=dOGnzWwUC/vE1aF6D8Thv9g4AdQoSYVmj2tPRjv3aYVDGzNP/XM+/xSkCTtwBzarL6 harCBxUH0i2YDAr9/rIh2ftGeDfw3CmYQGR4fBJaJQQvS2vsUofcaaioK3O0nK9pa4y4 +bikLn1IOiSms9ZYBSCIAR9TwTUBXjpKBBdlWPdtkABiMNO+KSAQJ+ojNCAgYD6GqN+i kjDsgo8L+E9DhQ0kkykfoXpZPGmTagh35a1+uXid2DtLCz1qqTvQO/xtA2zTscSVTkEq jf47PPf4y64yVJQmKg72f2eTA6M3sOZiI4g8ZxDawYG4kW53R8a//+HPtHAse6iJIf6n 5fRA== |
| X-Received | by 10.68.110.195 with SMTP id ic3mr8223910pbb.124.1427207050474; Tue, 24 Mar 2015 07:24:10 -0700 (PDT) |
| MIME-Version | 1.0 |
| In-Reply-To | <8a6dcb51-250d-4abc-abc9-68798f07af86@googlegroups.com> |
| References | <CACT3xuVF-MOVQK5wSorRSAfpAQEXvS7QsqarACVZKXsQ0bxzpQ@mail.gmail.com> <CAPTjJmrRv+DQGpzdY5te-vk_u9p5AzWYQ-6B=m1knPoaXpRmRQ@mail.gmail.com> <55105EF4.2070805@davea.name> <meq5i2$53s$1@ger.gmane.org> <mailman.93.1427174452.10327.python-list@python.org> <8a6dcb51-250d-4abc-abc9-68798f07af86@googlegroups.com> |
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Date | Tue, 24 Mar 2015 08:23:29 -0600 |
| Subject | Re: fibonacci series what Iam is missing ? |
| To | Python <python-list@python.org> |
| 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 <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.103.1427207059.10327.python-list@python.org> (permalink) |
| Lines | 62 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1427207059 news.xs4all.nl 2899 [2001:888:2000:d::a6]:56593 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:87885 |
Show key headers only | View raw
On Tue, Mar 24, 2015 at 12:20 AM, Rustom Mody <rustompmody@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(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
>
> This is rather arcane!
> What are the identities used above?
It's essentially the same matrix recurrence that Gregory Ewing's
solution uses, but without using numpy (which doesn't support
arbitrary precision AFAIK) and with a couple of optimizations.
The Fibonacci recurrence can be expressed using linear algebra as:
F_1 = [ 1 0 ]
T = [ 1 1 ]
[ 1 0 ]
F_(n+1) = F_n * T
I.e., given that F_n is a vector containing fib(n) and fib(n-1),
multiplying by the transition matrix T results in a new vector
containing fib(n+1) and fib(n). Therefore:
F_n = F_1 * T ** (n-1)
The code above evaluates this expression by multiplying F_1 by powers
of two of T until n-1 is reached. x and y are the two elements of the
result vector, which at the end of the loop are fib(n) and fib(n-1).
a, b, and c are the three elements of the (symmetric) transition
matrix T ** p, where p is the current power of two.
The last two lines of the loop updating a, b, and c could equivalently
be written as:
a, b, c = a*a + b*b, a*b + b*c, b*b + c*c
A little bit of algebra shows that if a = b + c before the assignment,
the equality is maintained after the assignment (in fact the elements
of T ** n are fib(n+1), fib(n), and fib(n-1)), so the two
multiplications needed to update a can be optimized away in favor of a
single addition.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Re: fibonacci series what Iam is missing ? Ian Kelly <ian.g.kelly@gmail.com> - 2015-03-23 23:20 -0600
Re: fibonacci series what Iam is missing ? Rustom Mody <rustompmody@gmail.com> - 2015-03-23 23:20 -0700
Re: fibonacci series what Iam is missing ? Rustom Mody <rustompmody@gmail.com> - 2015-03-23 23:28 -0700
Unicode magic (was fibonacci series what Iam is missing ?) Rustom Mody <rustompmody@gmail.com> - 2015-03-23 23:33 -0700
Re: fibonacci series what Iam is missing ? Ian Kelly <ian.g.kelly@gmail.com> - 2015-03-24 08:23 -0600
Re: fibonacci series what Iam is missing ? CHIN Dihedral <dihedral88888@gmail.com> - 2015-03-24 10:22 -0700
csiph-web