Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #79445

Re: (-1)**1000

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'value,': 0.04; 'interpreter': 0.05; 'binary': 0.07; 'odd': 0.07; '22,': 0.09; 'calculating': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; "wouldn't": 0.14; 'ast': 0.16; 'exponent.': 0.16; 'iterated': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'tried:': 0.16; 'accordingly.': 0.16; 'so.': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'skip:1 30': 0.19; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'integer': 0.24; 'skip:n 60': 0.24; 'header:X-Complaints-To:1': 0.27; 'header:In- Reply-To:1': 0.27; 'am,': 0.29; 'skip:( 40': 0.30; 'program,': 0.31; '>>>>': 0.31; 'clever': 0.31; 'explained': 0.31; 'something': 0.35; 'doing': 0.36; 'to:addr:python-list': 0.38; 'fact': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'even': 0.60; 'here:': 0.62; 'more': 0.64; 'url:a': 0.72; 'potentially': 0.81; 'parity': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Ned Batchelder <ned@nedbatchelder.com>
Subject Re: (-1)**1000
Date Wed, 22 Oct 2014 11:01:28 -0400
References <54476a77$0$21664$426a74cc@news.free.fr> <mailman.15058.1413968065.18130.python-list@python.org> <5447786d$0$12750$426a74cc@news.free.fr>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host 18.189.53.17
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Thunderbird/24.6.0
In-Reply-To <5447786d$0$12750$426a74cc@news.free.fr>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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.15076.1413990100.18130.python-list@python.org> (permalink)
Lines 38
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1413990100 news.xs4all.nl 2857 [2001:888:2000:d::a6]:47959
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:79445

Show key headers only | View raw


On 10/22/14 5:27 AM, ast wrote:
>
> "Chris Angelico" <rosuav@gmail.com> a écrit dans le message de
> news:mailman.15058.1413968065.18130.python-list@python.org...
>> On Wed, Oct 22, 2014 at 7:27 PM, ast <nomail@invalid.com> wrote:
>>> If i am writing (-1)**1000 on a python program, will the
>>> interpreter do (-1)*(-1)*...*(-1) or something clever ?
>>>
>>> In fact i have (-1)**N with N an integer potentially big.
>>
>> Exponentiation is far more efficient than the naive implementation of
>> iterated multiplication.
>
> In the very particular case of (-1)**N,  I belive that Python check
> the odd or even parity of N and provides the result accordingly.
>
> I tried:
>>>> (-1)**1000000000000000000000000000000000
> 1
>>>> (-1)**1000000000000000000000000000000001
> -1
>
> and it is instantaneous
>
>

Keep in mind that actually calculating the exponentiation wouldn't do 
1000000000000000000000000000000000 multiplications anyway: the clever 
way to do integer powers is by squaring based on the binary 
representation of the exponent.  It's explained here: 
http://stackoverflow.com/a/101613/14343

So even if Python is actually calculating the value, it's only doing 75 
multiplications or so.

-- 
Ned Batchelder, http://nedbatchelder.com

Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

(-1)**1000 "ast" <nomail@invalid.com> - 2014-10-22 10:27 +0200
  Re: (-1)**1000 Chris Angelico <rosuav@gmail.com> - 2014-10-22 19:54 +1100
    Re: (-1)**1000 "ast" <nomail@invalid.com> - 2014-10-22 11:27 +0200
      Re: (-1)**1000 Peter Otten <__peter__@web.de> - 2014-10-22 12:29 +0200
      Re: (-1)**1000 Michiel Overtoom <motoom@xs4all.nl> - 2014-10-22 12:38 +0200
      Re: (-1)**1000 Tim Chase <python.list@tim.thechases.com> - 2014-10-22 05:43 -0500
      Re: (-1)**1000 Peter Otten <__peter__@web.de> - 2014-10-22 13:02 +0200
      Re: (-1)**1000 Ian Kelly <ian.g.kelly@gmail.com> - 2014-10-22 08:23 -0600
      Re: (-1)**1000 Ian Kelly <ian.g.kelly@gmail.com> - 2014-10-22 08:29 -0600
      Re: (-1)**1000 Ned Batchelder <ned@nedbatchelder.com> - 2014-10-22 11:01 -0400
    Re: (-1)**1000 wxjmfauth@gmail.com - 2014-10-22 04:35 -0700
  Re: (-1)**1000 Peter Otten <__peter__@web.de> - 2014-10-22 11:13 +0200
  Re: (-1)**1000 Terry Reedy <tjreedy@udel.edu> - 2014-10-22 13:15 -0400
  Re: (-1)**1000 Dave Angel <davea@davea.name> - 2014-10-24 13:57 -0400
  Re: (-1)**1000 Tony the Tiger <tony@tiger.invalid> - 2014-10-26 20:07 +0000
    Re: (-1)**1000 Ned Batchelder <ned@nedbatchelder.com> - 2014-10-26 16:45 -0400
    Re: (-1)**1000 Stefan Behnel <stefan_ml@behnel.de> - 2014-10-29 20:31 +0100

csiph-web