Python Forum
Funny regex to find primes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Funny regex to find primes
#1
The idea came from this video: https://www.youtube.com/watch?v=5vbk0TwkokM
He refers also to this very detailed explanation: https://illya.sh/the-codeumentary-blog/r...-is-prime/

The regex ^.?$|^(..+?)\1+$ returns False if the digit is a prime.
As representation, the digit 1 is used, but any symbol could be used.

Example Code with 🎃 as symbol.
import re
from itertools import count

# Python 3.13.0 compatible, not tested without gil (3.13.0t)
import sympy

# to make it very clear, it can by any unicode character with the len 1
SYMBOL = "🎃"

if len(SYMBOL) != 1:
    raise RuntimeError(f"Symbol {SMYBOL} is not length == 1")


def is_prime(value: int) -> bool:
    """
    Video:
    https://www.youtube.com/watch?v=5vbk0TwkokM

    Link to the blog post:
    https://illya.sh/the-codeumentary-blog/regular-expression-check-if-number-is-prime/
    """
    return not re.match(r"^.?$|^(..+?)\1+$", SYMBOL * value)


def first_100_primes() -> list[int]:
    primes = []
    for n in count(2):
        if len(primes) >= 100:
            return primes
        if is_prime(n):
            primes.append(n)


primes = first_100_primes()

# safety check
for prime in primes:
    if not sympy.isprime(prime):
        raise ValueError(f"The number {prime} is not a prime")

for index in range(0, len(primes) + 1, 10):
    if chunk := primes[index : index + 10]:
        for prime in chunk:
            print(f"{prime:>3}", end=" ")
        print()
Output:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541
PS: As he explains in the Video, the order of the operation of the regex matching algorithm depends on the implementation. There is internally heavy optimization going on, which leads into different execution of the recursive matching. This code should not be used to check prime numbers.
Larz60+ likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  wanted: regex or code to find valide def statements in a line Skaperen 13 7,779 Mar-20-2020, 11:54 PM
Last Post: Skaperen
  a funny code nzcan 0 2,685 Oct-31-2018, 08:51 PM
Last Post: nzcan
  Funny English Kebap 41 31,394 Feb-10-2018, 02:06 AM
Last Post: Skaperen
  a funny coding challege Skaperen 2 3,957 Dec-07-2017, 02:26 AM
Last Post: Skaperen
  Funny things Java Devs do but Python Devs Don't QueenSvetlana 1 4,082 Nov-02-2017, 08:45 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020