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


Groups > comp.lang.python > #52458

Re: Question about function failing with large number

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.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; 'else:': 0.03; 'operator': 0.03; 'root': 0.05; 'cure': 0.07; 'float': 0.07; 'method.': 0.07; 'odd': 0.07; 'reason,': 0.07; 'subject:Question': 0.07; 'arguments': 0.09; 'bits': 0.09; 'line:': 0.09; 'method,': 0.09; 'pgp': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'statements': 0.09; 'subject:number': 0.09; 'thrown': 0.09; 'runs': 0.10; 'python': 0.11; 'def': 0.12; 'random': 0.14; 'times,': 0.14; ' -----begin': 0.16; '2):': 0.16; 'algorithm.': 0.16; 'bits.': 0.16; 'hash:': 0.16; 'length)': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'traceback.': 0.16; 'true:': 0.16; 'wrote:': 0.18; 'message-----': 0.19; 'version.': 0.19; 'starts': 0.20; 'code,': 0.22; 'import': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'integer': 0.24; 'specify': 0.24; 'signed': 0.27; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'testing': 0.29; 'converting': 0.30; "i'm": 0.30; '(which': 0.31; 'program,': 0.31; '3.x': 0.31; 'thanks!': 0.32; 'run': 0.32; 'skip:# 10': 0.33; 'trouble': 0.34; 'problem': 0.35; 'subject:with': 0.35; "can't": 0.35; 'case,': 0.35; 'convert': 0.35; 'test': 0.35; 'but': 0.35; 'done,': 0.36; 'false': 0.36; 'next': 0.36; 'charset:us-ascii': 0.36; 'should': 0.36; 'error.': 0.37; 'positive': 0.37; 'too': 0.37; 'being': 0.38; 'to:addr:python-list': 0.38; 'quote': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'enough': 0.39; 'received:org': 0.40; 'even': 0.60; 'then,': 0.60; 'numbers': 0.61; 'entire': 0.61; 'range': 0.61; "you're": 0.61; 'first': 0.61; "you'll": 0.62; 'telling': 0.64; 'effectively': 0.66; 'number:': 0.66; 'believe': 0.68; '600': 0.68; 'limit': 0.70; 'below.': 0.71; 'safe': 0.72; 'prime': 0.74; 'square': 0.74; '(always': 0.84; 'ram,': 0.84; 'start.': 0.84; 'sudden,': 0.84; 'working,': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Dave Angel <davea@davea.name>
Subject Re: Question about function failing with large number
Date Tue, 13 Aug 2013 12:57:12 +0000 (UTC)
References <520A2295.5040605@gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=US-ASCII
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host 174.32.174.30
User-Agent XPN/1.2.6 (Street Spirit ; Linux)
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.535.1376398654.1251.python-list@python.org> (permalink)
Lines 98
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1376398654 news.xs4all.nl 15906 [2001:888:2000:d::a6]:56599
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:52458

Show key headers only | View raw


Anthony Papillion wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA512
>
> 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

In general, you should quote the entire traceback.  it's no trouble in
this case, as the problem is clearly in the n**0.5 portion.  You should
also specify the Python version.  But it's a safe bet that you're using
2.x, since your print statements are illegal in 3.x
>
> The error is being thrown on this line:
>
> for x in range(3, int(n**0.5)+1, 2):
>
> The odd thing is that the error is not thrown every single time. I can
> run the function a few times, it will generate a large number (always
> the same length) and run it through the function. After I do this a
> few times, it fails with the error. I might get the error on the next
> few runs but then, all of a sudden, it functions again.
>
> Any ideas? The entire program, including the method, is below.
>
> #!/usr/bin/env python
>
> from random import getrandbits
>
> bits = 2048
>
> # Test if the number is a prime
> def isprime(n):
>
> 	# make sure n is a positive integer
> 	n = abs(int(n))
> 	# 0 and 1 are not primes
> 	if n < 2:
> 		return False
> 	# 2 is the only even prime number
> 	if n == 2:
> 		return True
> 	# all other even numbers are not primes
> 	if not n & 1:
> 		return False
> 	# range starts with 3 and only needs to go up the 		squareroot 		of 	n
> 	# for all odd numbers
> 	for x in range(3, int(n**0.5)+1, 2):
> 		if n % x == 0:
> 			return False
> 	return True
> 	
> a = getrandbits(bits)
> print "\nGenerated Number: ", a, "\n"
> print "Number of digits: ", len(str(a))
> isNumberPrime = isprime(a)
> if isNumberPrime == True:
> 	print "\nThis number is a prime.\n"
> else:
> 	print "\nThis number is not a prime.\n"
>
> Thanks!
> Anthony
>

The operator ** works by first converting its arguments to float.  if
the long value is too large for a Python float (which is a C double),
then you get this error.  You'll get the same one with math.sqrt().
Nothing you can do about that unless you want to write your own square
root function.

However, you can change your algorithm.  You'll need to anyway, since a
range that large would take a ridiculous amount of RAM, if it even was
willing to start.

The cure is to use a while loop, and test x*x agains the limit

while x*x < n:
    if n%x == 0:
        return False
    n+=2

BTW, I can't believe that getrandbits(2048) is going to be small enough
to 'work" for any noticeable probability.  So when you find it working,
perhaps you're using a smaller value for bits.

BTW, when you get this all done, you'll find that testing a prime of
600 digits is going to take an effectively infiinite time.  You need a
faster method.



-- 
DaveA

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


Thread

Re: Question about function failing with large number Dave Angel <davea@davea.name> - 2013-08-13 12:57 +0000

csiph-web