Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #52457
| Path | csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.005 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'float': 0.07; 'reason,': 0.07; 'rename': 0.07; 'subject:Question': 0.07; '"if': 0.09; '32-bit': 0.09; 'integers': 0.09; 'line:': 0.09; 'subject:number': 0.09; 'thrown': 0.09; 'python': 0.11; '(note': 0.16; '2):': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'inclined': 0.16; 'precision.': 0.16; 'redundant.': 0.16; 'storing': 0.16; 'underlying': 0.16; 'wrote:': 0.18; 'bit': 0.19; "python's": 0.19; 'work,': 0.20; '(the': 0.22; '>>>': 0.22; 'code,': 0.22; 'aug': 0.22; 'error': 0.23; 'instance,': 0.24; 'integer': 0.24; 'certain': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; '13,': 0.31; 'division': 0.31; 'mod': 0.31; 'running': 0.33; "i'd": 0.34; 'could': 0.34; 'problem': 0.35; 'subject:with': 0.35; 'something': 0.35; 'convert': 0.35; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'false': 0.36; 'positive': 0.37; 'too': 0.37; 'being': 0.38; 'to:addr:python- list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'how': 0.40; 'even': 0.60; 'break': 0.61; 'lower': 0.61; 'numbers': 0.61; 'range': 0.61; 'simply': 0.61; "you'll": 0.62; "you've": 0.63; 'telling': 0.64; 'more': 0.64; 'useful.': 0.68; 'prime': 0.74; 'square': 0.74; 'built,': 0.84; 'divide': 0.84; "it'd": 0.84; '2013': 0.98 |
| 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:to :content-type; bh=+Te+awY7c46tdGvUMQwi2ELoTrLj43e8veVl5ynw8uc=; b=zHTF905O2GBoCttpyAK+w4150P67aJi9y4Qo858+bHS1NP+tjMGqZTf/4BEWERkqpu PO7xiUJ00HCDuguQ4P//W/rSzEil2FuahPDIfs446U5VzO2h/j/9EabsRrsQxcW5CBJa 32d+rSHEnd0ZXgFiuauWo45CCJUR85RPZYqlGBXB0rF0WOabZ1Xx4IPMVKUXkBimn/NG +8JTzaMe+NPwfPxHgjf/E1jUcnRaqXT8k380sUCQ3lUT5rxpj/aHg6FsV1XGrb9ixSGw D5quLCpPGwFJusIp14eQo9TLJ90vj/iRY4Kk610ch2LxBDoyFGy8yR6V4NEOWP9ELdcj VJuA== |
| MIME-Version | 1.0 |
| X-Received | by 10.220.164.202 with SMTP id f10mr4340241vcy.25.1376397776325; Tue, 13 Aug 2013 05:42:56 -0700 (PDT) |
| In-Reply-To | <520A2295.5040605@gmail.com> |
| References | <520A2295.5040605@gmail.com> |
| Date | Tue, 13 Aug 2013 13:42:56 +0100 |
| Subject | Re: Question about function failing with large number |
| From | Chris Angelico <rosuav@gmail.com> |
| To | python-list@python.org |
| Content-Type | text/plain; charset=ISO-8859-1 |
| 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 | <http://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 | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.534.1376397784.1251.python-list@python.org> (permalink) |
| Lines | 60 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1376397784 news.xs4all.nl 15876 [2001:888:2000:d::a6]:51315 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:52457 |
Show key headers only | View raw
On Tue, Aug 13, 2013 at 1:12 PM, Anthony Papillion <papillion@gmail.com> wrote:
> So I'm using the function below to test a large (617 digit) number for
> primality. For some reason, when I execute the code, I get an error
> telling me:
>
> OverflowError: long int too large to convert to float
>
> The error is being thrown on this line:
>
> for x in range(3, int(n**0.5)+1, 2):
Python's integers are unbounded, storing arbitrary precision. Python's
floats are limited to the range of the underlying IEEE implementation.
You'll have a certain cutoff above which your system bombs.
>>> float(1<<1023)
8.98846567431158e+307
>>> float(1<<1024)
Traceback (most recent call last):
File "<pyshell#68>", line 1, in <module>
float(1<<1024)
OverflowError: long int too large to convert to float
(The exact cutoff may depend on how your Python is built, I think;
that was running on a 32-bit Python on Windows.)
Everything else in your code will work, so your only problem is this
square rooting. So here's an alternative:
for x in range(3, n, 2):
div, mod = divmod(n, x)
if mod == 0:
return False
if div < n:
break
Once the quotient is lower than the divisor, you've passed the square
root. (Note the use of divmod to do a single division and return both
quotient and remainder. You could alternatively use // and % but it'd
divide twice.)
By the way, the "== True" in your final condition is redundant. Just
test "if isNumberPrime:", or even just "if isprime(a):". :) Though
with something like this that simply returns boolean, I'd be inclined
to make it return a bit more - for instance, it returns the first-seen
factor.
if n < 2:
return 1 # Only way this will ever return a non-prime integer
if n == 2:
return None # Prime!
if not n & 1:
return 2
# and inside the loop: "return x"
Rename the function to "iscomposite", because prime numbers return
None (false) and composites return a positive integer (true), and
you've just made the function that bit more useful. :)
ChrisA
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Question about function failing with large number Chris Angelico <rosuav@gmail.com> - 2013-08-13 13:42 +0100
csiph-web