User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 402,505 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,793 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums
Mar 3rd, 2007
Views: 7,280
This simple isprime(number) function checks if the given integer number is a prime number and returns True or False. The function makes sure that the number is a positive integer, and that 1 is not considered a prime number.
Last edited : Apr 28th, 2007.
python Syntax | 4 stars
  1. # prime numbers are only divisible by unity and themselves
  2. # (1 is not considered a prime number by convention)
  3.  
  4. def isprime(n):
  5. '''check if integer n is a prime'''
  6. # make sure n is a positive integer
  7. n = abs(int(n))
  8. # 0 and 1 are not primes
  9. if n < 2:
  10. return False
  11. # 2 is the only even prime number
  12. if n == 2:
  13. return True
  14. # all other even numbers are not primes
  15. if not n & 1:
  16. return False
  17. # range starts with 3 and only needs to go up the squareroot of n
  18. # for all odd numbers
  19. for x in range(3, int(n**0.5)+1, 2):
  20. if n % x == 0:
  21. return False
  22. return True
  23.  
  24. # test ...
  25. print isprime(1) # False
  26. print isprime(2) # True
  27. print isprime(3) # True
  28. print isprime(29) # True
  29. print isprime(345) # False
  30. print isprime(999979) # True
  31. print isprime(999981) # False
Comments (Newest First)
slate | Light Poster | Jun 7th, 2008
Simple RSA primtest. Does not work on universal pseudo primes.

  1. def isprime(n,PROB):
  2. '''returns if the number is prime. Failure rate: 1/4**PROB '''
  3. if n==2: return '1'
  4. if n==1 or n&1==0:return '0'
  5. s=0
  6. d=n-1
  7. while 1&d==0:
  8. s+=1
  9. d>>=1
  10. for i in range(PROB):
  11. a=random.randint(2,n-1)
  12. composit=True
  13. if pow(a,d,n)==1:
  14. composit=False
  15. if composit:
  16. for r in xrange(0,s):
  17. if pow(a,d*2**r,n)==n-1:
  18. composit=False
  19. break
  20. if composit: return False
  21. return True
Toster | Newbie Poster | Jun 3rd, 2008
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

  1. def isprime(n):
  2. if n == 2:
  3. return 1
  4. if n % 2 == 0:
  5. return 0
  6.  
  7. max = n**0.5+1
  8. i = 3
  9.  
  10. while i <= max:
  11. if n % i == 0:
  12. return 0
  13. i+=2
  14.  
  15. return 1
theApprentice() | Newbie Poster | Sep 19th, 2007
Hi,

The code listed above does not seem accurate as numbers like 49 and 95 pass the test and yet they are not prime numbers!

Perhaps a flaw in the algorithm used?
vegaseat | Kickbutt Moderator | Apr 28th, 2007
Filtered out even numbers, except number 2, to speed things up.
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 5:47 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC