Peteris Krumins personal blog

A Perl Regular Expression That Matches Prime Numbers

Take a look at this Perl regular expression: perl -lne '(1x$_) =~ /^(11+?)\1+$/ && print "$_ is composite"' This regular expression matches only the composite numbers (and doesn't match prime numbers). Can you figure out how it works? I'll give the explanation below but try to figure it…
Points
123
HN comments
27
Submissions
1
Cited by
0 commenters

Software EngineeringMath & Algorithms2011

On Hacker News

SubmittedTitle on HNPointsComments
Dec 25, 2011A Perl Regular Expression That Matches Prime Numbers by pkrumins12327

More from Peteris Krumins

All 57 posts →

Related posts from the same era