video thumbnail 18:36
How on Earth does ^.?$|^(..+?)\1+$ produce primes?

2024-10-31

[public] 247K views, 23.6K likes, dislikes audio only

4K

Take your personal data back with Incogni! Use code standupmaths at the link below and get 60% off an annual plan: https://incogni.com/standupmaths

^.?$|^(..+?)\1+$

Props will be for sale here: https://www.ebay.co.uk/usr/standupmaths

Fantastic blog post by Illya Gerasymchuk. "Demystifying The Regular Expression That Checks If A Number Is Prime" https://illya.sh/the-codeumentary-blog/regular-expression-check-if-number-is-prime/

Thanks to everyone who has sent in history about this regex. Seems it was written ≥25 years ago by a Perl programmer named Abigail.

Cheers to viewer Dave Cross who found this earliest-known mention in a 2000 blog post: http://test.neilk.net/blog/2000/06/01/abigails-regex-to-test-for-prime-numbers/

Thanks to viewers Chris Lawrence and Jimmy Diep for suggesting the topic.

Huge thanks to my Patreon supporters. They keep me supplied with all the primes I need. https://www.patreon.com/standupmaths

CORRECTIONS

- Several people have corrected me that regex is zero indexed it’s just that \0 refers to the entire matched string.

- Mike Salisbury ands others have pointed out that the left-most column in the table of repeated wild cards shouldn't be included. Otherwise the length of the string could match a single prime length.

- At 6:07 I say "Begins and end with an m" when I meant ends with an “s”. No one has corrected that mistake with more vigour than viewer Timur Sultanov.

- There is some concern about my pronunciation of rejex.

- Let me know if you spot anything else!

Filming, VFX and editing by Alex Genn-Bash

Written and performed by Matt Parker

Extra material by Sam Hartburn

Number yeeting by Lucie Green

Spooky lighting and producing by Nicole Jacobus

Music by Howard Carter

Design by Simon Wright and Adam Robinson

MATT PARKER: Stand-up Mathematician

Website: http://standupmaths.com/


Stand-up Maths I do mathematics and stand-up. Sometimes simultaneously. Occasionally while being filmed. (It's quite the Venn diagram.) You can send business stuff to standupmaths@zigguratxyz.com and maths to matt@standupmaths.com (but everything gets to me one way or another). I'm also in these places: http://standupmaths.com http://twitter.com/standupmaths https://m.bilibili.com/space/475271755
/youtube/channel/UCSju5G2aFaWMqn-_0YBtq5A
What do we know about Asteroid 314159 aka ‘Mattparker’? 199,893 views
/youtube/video/GyNbLtiAgj4