regular expressions

Regular Expression To Check For Prime Numbers

The code is in python, but the concept is the same for any language with regular expression support (perl, sed, awk, vim, etc..)

1
2
3
import re
def is_prime(n):
        return re.match(r'^1?$|^(11+?)\1+$', "1" * n) == None;

That’s it :) using a regular expression we can check if a number is prime or not.

Here is a short explanation of why it works (though it will ruin the magic part):
› Continue reading

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

Tags: ,

Saturday, July 24th, 2010 Regular Expressions 2 Comments
my email
my photo
Hi,
My name is Amir Watad. I have a BSc. in biomedical engineering from The Biomedical Engineering school , Technion , Israel, and a BSc. in electrical engineering from The Electrical Engineering school , Technion , Israel.
I'm a Verification Engineer in Mellanox Technologies Ltd.
I love Linux, the Command Line and the OpenSource Community.
I used to write Poems (Arabic) when I was able to find time for this.
February 2012
S M T W T F S
« Sep    
 1234
567891011
12131415161718
19202122232425
26272829  
SEO Powered by Platinum SEO from Techblissonline

Twitter links powered by Tweet This v1.7.4, a WordPress plugin for Twitter.