```
# prime numbers are only divisible by unity and themselves
# (1 is not considered a prime number by convention)
def isprime(n):
'''check if integer n is a prime'''
# 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
# test ...
print isprime(1) # False
print isprime(2) # True
print isprime(3) # True
print isprime(29) # True
print isprime(345) # False
print isprime(999979) # True
print isprime(999981) # False
# extra test ...
print isprime(49) # False
print isprime(95) # False
```

I think the mistake is that you converted the square root to an int, because the code I wrote a couple of years ago works fine, and its almost the same

```
def isprime(n):
if n == 2:
return 1
if n % 2 == 0:
return 0
max = n**0.5+1
i = 3
while i <= max:
if n % i == 0:
return 0
i+=2
return 1
```

Editors note: the range function in vegaseat's code needs integers, so it's correct.

0

Simple RSA primtest. Does not work on universal pseudo primes.

```
def isprime(n,PROB):
'''returns if the number is prime. Failure rate: 1/4**PROB '''
if n==2: return '1'
if n==1 or n&1==0:return '0'
s=0
d=n-1
while 1&d==0:
s+=1
d>>=1
for i in range(PROB):
a=random.randint(2,n-1)
composit=True
if pow(a,d,n)==1:
composit=False
if composit:
for r in xrange(0,s):
if pow(a,d*2**r,n)==n-1:
composit=False
break
if composit: return False
return True
```

0

this works fine returning true if prime and false with the smallest number that divides it if it is false.

```
def isprime(x):
x = abs(int(x))
if x < 2:
return "Less 2", False
elif x == 2:
return True
elif x % 2 == 0:
return False
else:
for n in range(3, int(x**0.5)+2, 2):
if x % n == 0:
return n, False
return True
```

0

OK, here is a really optimized version for this classic problem. This is not originally mine (but have done very similar).

It utilizes the fact that primes > 4 are 1(mod 6) or 5(mod 6). Actually I editted it a lot now before posting to be more according to format standards of PEP8.

```
def is_prime(n):
if n == 2 or n == 3:
return True
elif n < 2 or n % 2 == 0:
return False
elif n < 9:
return True
elif n % 3 == 0:
return False
r = int(sqrt(n))
f = 5
while f <= r:
if n % f == 0 or n % (f + 2) == 0:
return False
else:
f += 6
return True
```

0

Super fast for very large numbers:

```
def miller_rabin_isprime(a, i, n):
"""
Miller-Rabin primality test
returns a 1 if n is a prime
usually i = n - 1
does not test prime 2
"""
if i == 0:
return 1
x = miller_rabin_isprime(a, i // 2, n)
if x == 0:
return 0
y = (x * x) % n
if ((y == 1) and (x != 1) and (x != (n - 1))):
return 0
if (i % 2) != 0:
y = (a * y) % n
return y
```

-2

I just got an automated email from Daniweb suggesting that I read this thread based on my recent activity. The code posts in this thread are fine, but most of the comment posts are curmudgeonly, rude, standoffish, exclusivist, and socially inept. People seem to think it's a violation of some natural law if a beginner programmer joins a discussion and tries to make a positive contribution by posting code. I'll definitely be putting some of you on my ignore list.

0

People seem to think it's a violation of some natural law if a beginner programmer joins a discussion and tries to make a positive contribution by posting code.

On the contrary, we answer beginner programmer's questions every day in the python forum and try to help them as much as we can. Correct errors and inefficiencies in code is not rude or socially inept: we all learn by our own mistakes.

I would advise beginner programmers to start their own thread with well described issues to solve. They will probably get better answers than by adding to a code snippet thread.

They may also find many ideas in Vegaseat's thread projects-for-the-beginner.

Isn't it about time forums rewarded their contributors?

Earn rewards points for helping others. Gain kudos. Cash out. Get better answers yourself.

It's as simple as contributing editorial or replying to discussions labeled OP Sponsor or OP Kudos

You

This is an OP Kudos discussion and contributors
may be rewarded