Created
October 23, 2020 16:49
-
-
Save HemersonTacon/829386b8dff8f76f39208239b4064e49 to your computer and use it in GitHub Desktop.
Find out if a number is prime with regex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
""" | |
Created on Fri Oct 23 17:34:25 2020 | |
@author: hemerson.tacon | |
""" | |
import re | |
def is_prime(n): | |
return re.match(r"^1?$|^(11+?)\1+$", "1"*n) == None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Reference and explanation: https://itnext.io/a-wild-way-to-check-if-a-number-is-prime-using-a-regular-expression-4edfb725f895