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


Groups > comp.lang.python > #80072

Re: Challenge: optimizing isqrt

Path csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-1.proxad.net!hq-usenetpeers.eweka.nl!hq-usenetpeers.eweka.nl!bcyclone03.am1.xlned.com!bcyclone03.am1.xlned.com!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.027
X-Spam-Evidence '*H*': 0.95; '*S*': 0.00; 'cpython': 0.05; 'float': 0.07; 'test,': 0.07; 'core,': 0.09; 'linear': 0.09; 'worse': 0.09; 'cc:addr:python-list': 0.11; 'bounds.': 0.16; 'discussion.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'hmm.': 0.16; 'linear,': 0.16; 'low.': 0.16; 'reasonably': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'seems': 0.21; 'import': 0.22; 'cc:addr:python.org': 0.22; 'hours,': 0.24; 'math': 0.24; 'cc:2**0': 0.24; 'least': 0.26; 'header:In-Reply- To:1': 0.27; 'correct': 0.29; 'message-id:@mail.gmail.com': 0.30; 'largest': 0.30; "i'm": 0.30; "d'aprano": 0.31; 'steven': 0.31; 'probably': 0.32; 'maybe': 0.34; "i'd": 0.34; 'something': 0.35; 'case,': 0.35; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'much.': 0.36; 'done': 0.36; 'doing': 0.36; 'next': 0.36; 'method': 0.36; 'similar': 0.36; 'should': 0.36; 'so,': 0.37; 'too': 0.37; 'two': 0.37; 'being': 0.38; 'confirmed': 0.38; 'mine': 0.38; 'nov': 0.38; 'pm,': 0.38; 'expect': 0.39; 'above,': 0.60; 'break': 0.61; 'numbers': 0.61; 'took': 0.61; "you're": 0.61; 'high': 0.63; 'such': 0.63; 'more': 0.64; 'within': 0.65; 'hours': 0.66; 'minutes': 0.67; 'between': 0.67; 'low': 0.83; 'high,': 0.84; 'technically': 0.84; 'probe': 0.91; 'to:none': 0.92
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=4Yi0p2p1sp7wxJjNN/jlBtPM2P5gzuZbyeX5Pn2O7NQ=; b=JyAf5BGVwYu6f6WftIOagSB74q+44PViBqAAEuKFp071DwdX0zrXl27t5Gh3tYUkGd XcKZXSik2OGNM4KQX81T6HMAw9AvgxOFKWmXN89/FoFHWrXejmJ5u/tBUxWpVieob3uj dMXhJA0vi/nS3bCukJvh84zyw8m6yZjO0eJd8t3VwK5oCwjXVprwvvY973uprWZgbMUw c+ylp5UEg2dzTJSALE7dWAzmr7kYyOes+ZMTNpJh8ecHLaW9RRqIVJwsayRB/DGoPNmg fRI0n1jUYZ9lnpQO8e9tkGGHqNUKRgaL3qeIsR/6n4w6vKuqjVnHc9mY1LMvdhv+O95S DsEg==
MIME-Version 1.0
X-Received by 10.50.61.209 with SMTP id s17mr1070130igr.34.1414807974529; Fri, 31 Oct 2014 19:12:54 -0700 (PDT)
In-Reply-To <54543787$0$13005$c3e8da3$5496439d@news.astraweb.com>
References <54543787$0$13005$c3e8da3$5496439d@news.astraweb.com>
Date Sat, 1 Nov 2014 13:12:54 +1100
Subject Re: Challenge: optimizing isqrt
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
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.15376.1414807977.18130.python-list@python.org> (permalink)
Lines 50
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1414807977 news.xs4all.nl 2860 [2001:888:2000:d::a6]:60233
X-Complaints-To abuse@xs4all.nl
X-Received-Body-CRC 3502124164
X-Received-Bytes 5830
Xref csiph.com comp.lang.python:80072

Show key headers only | View raw


On Sat, Nov 1, 2014 at 12:29 PM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> Q1: What is the largest value of M, such that
>
> all(isqrt(i) == int(math.sqrt(n)) for n in range(M))
>
> returns True?
>
> I have done a brute force test, and in nine hours confirmed that M is at
> least 7627926244. That took nine hours, and I expect that a brute force
> test of every int representable as a float would take *months* of
> processing time.

Here's something that should be faster than pure brute-force:

import math
for i in range(2**34): # 2**34 should take about nine hours
    sqrt = int(math.sqrt(i))
    if sqrt * sqrt > i:
        print("Too high at %d" % i)
        break
    if (sqrt+1) * (sqrt+1) <= i:
        print("Too low at %d" % i)
        break

There are two ways int(math.sqrt(i)) can be wrong: it can return a
number that's too high, or one that's too low. If it's too high,
squaring it (btw, int*int seems to be faster than int**2) will produce
a number higher than the original. If the result was too low, then the
next number up would have been within the correct bounds. There's no
need to actually calculate isqrt here.

I was able to probe up to 2**29 in seven minutes on my computer
(allocating one core, CPython 3.5 from trunk). If it's linear, going
as far as 2**34 should take no more than the nine hours you spent. I
expect time will be a bit worse than linear (working with small
numbers is marginally faster than working with big numbers), but if
it's at least reasonably close, probing as far as 2**53 would take
81555 days. So, uhh, a brute force by your method is going to take a
lot more than months. (And 2**53 isn't technically where ints stop
being representable, but I'd use that as the final cut-off, as it's
where *all* ints stop being representable. In any case, there's a
failure at 2**54-1, so there's no need to go past there.)

Hmm. The speed difference between your brute force and mine isn't all
that much. I'm estimating maybe doing at best twice as much in the
same time. So you're probably already using something similar to the
above, and this isn't adding much to the discussion. :(

ChrisA

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


Thread

Challenge: optimizing isqrt Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-11-01 12:29 +1100
  Re: Challenge: optimizing isqrt Chris Angelico <rosuav@gmail.com> - 2014-11-01 13:12 +1100
  Re: Challenge: optimizing isqrt Christian Gollwitzer <auriocus@gmx.de> - 2014-11-01 09:02 +0100
    Re: Challenge: optimizing isqrt Christian Gollwitzer <auriocus@gmx.de> - 2014-11-01 09:09 +0100
    Re: Challenge: optimizing isqrt Chris Angelico <rosuav@gmail.com> - 2014-11-01 19:13 +1100
      Re: Challenge: optimizing isqrt Christian Gollwitzer <auriocus@gmx.de> - 2014-11-01 09:25 +0100
        Re: Challenge: optimizing isqrt Chris Angelico <rosuav@gmail.com> - 2014-11-01 19:33 +1100
          Re: Challenge: optimizing isqrt Christian Gollwitzer <auriocus@gmx.de> - 2014-11-01 09:38 +0100
            Re: Challenge: optimizing isqrt Chris Angelico <rosuav@gmail.com> - 2014-11-01 19:45 +1100
  Re: Challenge: optimizing isqrt Akira Li <4kir4.1i@gmail.com> - 2014-11-01 12:12 +0300
  Re: Challenge: optimizing isqrt Serhiy Storchaka <storchaka@gmail.com> - 2014-11-20 20:00 +0200
  Re: Challenge: optimizing isqrt Stephen Tucker <stephen_tucker@sil.org> - 2014-11-21 08:15 +0000
  Re: Challenge: optimizing isqrt Dave Angel <davea@davea.name> - 2014-11-23 10:43 -0500
  Re: Challenge: optimizing isqrt Serhiy Storchaka <storchaka@gmail.com> - 2014-11-25 21:31 +0200
  Re: Challenge: optimizing isqrt Dave Angel <davea@davea.name> - 2014-11-25 19:46 -0500
  Re: Challenge: optimizing isqrt Stephen Tucker <stephen_tucker@sil.org> - 2014-11-26 07:57 +0000
    Re: Challenge: optimizing isqrt Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-11-26 21:25 +1100
  Re: Challenge: optimizing isqrt Serhiy Storchaka <storchaka@gmail.com> - 2014-11-26 18:24 +0200

csiph-web