Logo

dev-resources.site

for different kinds of informations.

The interesting regex for Identifying Prime Numbers

Published at
6/28/2024
Categories
regex
math
Author
joaoreider
Categories
2 categories in total
regex
open
math
open
Author
10 person written this
joaoreider
open
The interesting regex for Identifying Prime Numbers

Prime numbers are numbers greater than 1 that have no positive divisors other than 1 and themselves.

The regex is ^1?$|^(11+?)\1+$

Let's breaking Down...

First you need to know the meaning of these 3 symbols:

  • ^: start of the string
  • $: end of the string
  • |: OR operator

So we have two components in the main regex:

1?: The literal โ€œ1โ€ and the question mark "?" for match the previous char zero or one times. So if we have zero characters or โ€œ1โ€ it will match. Soon we will know the reason for this pattern here.

(11+?)\1+: The second pattern. (11+?) is a group and matches any string that starts with โ€œ11โ€ by one or more โ€œ1โ€s. The โ€œ+?โ€ makes it non-greedy, meaning it matches as few characters as possible.
\1+ capture the same text again with as many characters as possible.

E.g. So for the number '111111', the pattern '11' is repeated three times. For the number 5 ('11111'), there is no way to split it into repeated sub-patterns.

So the interesting thing is that we found how to evenly divide repeated sub patterns.
In the same number '111111', the pattern '111' is also repeated twice and this is captured by regex.
For prime numbers, the string cannot be divided evenly into repeating sub patterns.

Ah, and first pattern (1?) handles the non-prime cases of 0 and 1.

Thank you for reaching this point. If you need to contact me, here is my email: [email protected]

Sapere aude

math Article's
30 articles in total
Favicon
Get 10th Class Math Notes (Matric Part 2) โ€“ Download Now for Free
Favicon
Why 1% - 1% Isn't Zero in Your Calculator (And What It Really Means)
Favicon
Homogeneous and Cartesian coordinates
Favicon
Sat Math bootcamp Accelerator AI Academy
Favicon
Math concepts explained with JavaScript
Favicon
How To Write MATH in DEV.to Articles
Favicon
Analysis of the length of optimal games of Hex game using alphazero-like AI
Favicon
Explore Best LLM of World Math Problems: Developers Solution
Favicon
[18/52] Engineering Fundamentals: Teh Maths
Favicon
Part II: Foundations of Logical Thinking in Programming, Logical Connectors
Favicon
Pragmatic Category Theory | Part 2: Composing Semigroups
Favicon
Statistics with R - Measures of Central Tendency and Measures of Dispersion
Favicon
Pragmatic Category Theory | Part 1: Semigroup Intro
Favicon
Cellular Automata - Wonders of Emergent Patterns from Biology to Computer Science
Favicon
Easy Approximations with Monte Carlo Simulations
Favicon
Lรณgica Matemรกtica
Favicon
Conjuntos
Favicon
functions and their inverses: 2 insightful examples.
Favicon
The interesting regex for Identifying Prime Numbers
Favicon
Foundations of Logical Thinking in Programming
Favicon
Linear Algebra
Favicon
What mathematics does the big model involve?
Favicon
Chapter 1: Boring SVG Manipulation
Favicon
Beyond Numbers: The Value of Studying Mathematics in College
Favicon
New binary Hadamard-like transform with avalanche effect
Favicon
๐Ÿš€ Excited to share my latest project: Math Runner! ๐ŸŽฎ๐Ÿ“š
Favicon
The Importance of Studying Mathematics in College
Favicon
Key mathematical formulas and concepts relevant to AI
Favicon
Obtaining the Cubic Hermite Spline Derivative on an Arbitrary Interval
Favicon
How to Over Engineer a Todo App (The Zenith Gradient Algorithm)

Featured ones: