Advertisement
maurobaraldi

Discovering Prime Numbers with Regular Expressions

Apr 5th, 2011
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.39 KB | None | 0 0
  1. #
  2. # Discovering Prime Numbers with Regular Expressions in Python
  3. # Inspired in post http://zmievski.org/2010/08/the-prime-that-wasnt? of Andrei Zmievski
  4. # Author: Mauro Baraldi mauro.baraldi@gmail.com
  5. #
  6.  
  7. import regex
  8.  
  9. def regex_prime_(n):
  10.     try:
  11.         return "%i is not prime" % re.match(r"^1?$|^(11+?)\1+$", '1'*n).group(0).count('1')
  12.     except:
  13.         return "%i is prime" % n
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement