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 at in.callcentric.com

XMPP: cypherpunk at patts.us

www.cajuntechie.org