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


Groups > comp.lang.python > #52454

Question about function failing with large number

Path csiph.com!usenet.pasdenom.info!news.albasani.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <papillion@gmail.com>
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; 'float': 0.07; 'odd': 0.07; 'reason,': 0.07; 'skip:l 60': 0.07; 'subject:Question': 0.07; 'bits': 0.09; 'line:': 0.09; 'method,': 0.09; 'pgp': 0.09; 'skip:t 60': 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; '-----end': 0.16; '2):': 0.16; 'hash:': 0.16; 'length)': 0.16; 'true:': 0.16; 'message-----': 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; 'signed': 0.27; 'function': 0.29; "i'm": 0.30; 'program,': 0.31; 'skip:i 60': 0.31; 'skip:k 60': 0.31; 'phone:': 0.31; 'thanks!': 0.32; 'run': 0.32; 'skip:# 10': 0.33; 'subject:with': 0.35; 'convert': 0.35; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'false': 0.36; 'next': 0.36; 'error.': 0.37; 'positive': 0.37; 'too': 0.37; 'being': 0.38; 'message-id:@gmail.com': 0.38; 'to:addr:python-list': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'even': 0.60; 'then,': 0.60; 'numbers': 0.61; 'entire': 0.61; 'range': 0.61; 'telling': 0.64; 'number:': 0.66; 'below.': 0.71; 'prime': 0.74; '(always': 0.84; 'sudden,': 0.84; 'skip:9 60': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject :content-type:content-transfer-encoding; bh=BMUC5NCPEU4nyHqAkW+ZAgP1YjqThpqmzw+0Voo+ITw=; b=Fq+nIDVKIP/PGuvqDDYD7SxxMHd3M/A48QgeRan7hpbuuxXqJuhTWokd7nMCVtTiRS ZPkF+c7aml23bLzLpRMTh7Ofa8wJMpyxt3aN99knolXnRhmK3psVSh4lDCVTKu8sP8r/ wJe8V/Pb3mObhJkYd/Ab/PDL9um8DUAqErT59RFuT6BKGkz33Erb8cIJcm2lUAVOSvCp lyfV1XTBAmNTU07Bucr23F/MdRs+aHzK+DOvIV7Zet+QsL8eylo2qEqyb2XEw8/BS9L4 XQZxIVxMQy5zZzmc+0DB7WlTztdEo+QSEYHtvDqTUR3KVW2ShA2r/XSdENWNjWRkeZO6 D3tw==
X-Received by 10.182.214.98 with SMTP id nz2mr3433235obc.37.1376395820510; Tue, 13 Aug 2013 05:10:20 -0700 (PDT)
Date Tue, 13 Aug 2013 07:12:05 -0500
From Anthony Papillion <papillion@gmail.com>
User-Agent Mozilla/5.0 (X11; Linux i686; rv:17.0) Gecko/20130625 Thunderbird/17.0.7
MIME-Version 1.0
To python-list@python.org
Subject Question about function failing with large number
X-Enigmail-Version 1.5.2
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding 7bit
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.532.1376395829.1251.python-list@python.org> (permalink)
Lines 84
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1376395829 news.xs4all.nl 15944 [2001:888:2000:d::a6]:41424
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:52454

Show key headers only | View raw


-----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

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


- -- 
Anthony Papillion
Phone:   1.918.533.9699
SIP:     17772098750@in.callcentric.com
XMPP:    cypherpunk@patts.us

www.cajuntechie.org
-----BEGIN PGP SIGNATURE-----

iQIcBAEBCgAGBQJSCiKNAAoJEKCKfnPhYIFFn7gP/A8CHOyTv71J/uVpMVYRcDRp
KRwbL9A1gkzUpycQibN3Q90FwY6nsGGPCeOkZByfYZnMKQH0o4Kd7QQf0hEOkhzO
BLiQjbkjzuUq7usE5TIqjTi0pJ6J7DcRm6U77yhAWrVt60MpfOucojLzX8ZolTD6
7Ha1gJ+9uEcwjosx1ynjVt7MQ/uGZwM7xS6WNfOpOwIYnoT5zBUzlKbw1HqSGYLu
6cWmAFTnnnXv6qymbGTxdZf0dxciODXy5xIMp5CzG6zIeHIOvjG03AbcY/+nI5FI
b9fKqjbblE/Npnh9GPXOLpI+I05VZMoO1b0AJSlU+Iq1liZAZOA4s2kf7XCrSb7Y
8Zn6qMPMTuNBPZpRJykTJSrA8s+4RxA0BWoq9rnTNXJPVR6imt6USOtwY4UssPxw
HIUrbSmfAEF9+/g08mcKHTVFstMyQCuAbUGx+LxoxkySnwZkfcwTRQ2vuoBDd7XP
92IJlkAdwepEa748P6NHNkSN4+OV3zAeTczHkzD0OL2KAeCuPY5tlqsI0MAngKOu
TIZrG+w1rvkz1gU3TBILLySuQTk/ioHNoVAH46bvp6ARRJiHJ4Ub61NoyooMCOgX
Lg+XtmcYz9VdUzdayg5uwNQAb/2/DboAyvmuuRDrA2Lzndv1JO3ye222/WCI9e7V
aB1ghvNnNMtrOWA0ZVQX
=BZUP
-----END PGP SIGNATURE-----

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


Thread

Question about function failing with large number Anthony Papillion <papillion@gmail.com> - 2013-08-13 07:12 -0500

csiph-web