473,387 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Regular Expression for Prime Numbers (or How I came to fail at them,and love the bomb)

I was reading up on this site [http://www.noulakaz.net/weblog/
2007/03/18/a-regular-expression-to-check-for-prime-numbers/] of an
interesting way to work out prime numbers using Regular Expression.

However my attempts to use this in Python keep returning none
(obviously no match), however I don't see why, I was under the
impression Python used the same RE system as Perl/Ruby and I know the
convert is producing the correct display of 1's...Any thoughts?

def re_prime(n):
import re
convert = "".join("1" for i in xrange(n))
return re.match("^1?$|^(11+?)\1+$", convert)

print re_prime(2)

Also on a side note the quickest method I have come across as yet is
the following

def prime_numbers(n):
if n < 3: return [2] if n == 2 else []
nroot = int(n ** 0.5) + 1
sieve = range(n + 1)
sieve[1] = 0
for i in xrange(2, nroot):
if sieve[i]:
sieve[i * i: n + 1: i] = [0] * ((n / i - i) + 1)
return [x for x in sieve if x]

Damn clever whoever built this (note sieve will produce a list the
size of your 'n' which is unfortunate)
Feb 13 '08 #1
8 2761
On Wed, 2008-02-13 at 10:40 -0800, me********@aol.com wrote:
But why doesn't it work when you make that change?
I can't answer that question, because it *does* work when you make that
change.

--
Carsten Haese
http://informixdb.sourceforge.net
Feb 13 '08 #2
On Feb 13, 12:53*pm, Carsten Haese <cars...@uniqsys.comwrote:
On Wed, 2008-02-13 at 10:40 -0800, mensana...@aol.com wrote:
But why doesn't it work when you make that change?

I can't answer that question, because it *does* work when you make that
change.
Well, the OP said the function was returning None which meant
no match which implies None means composite for the given example 2.

If None was supposed to mean prime, then why would returing None
for 2 be a problem?

But isn't this kind of silly?

## 3 None
## 4 <_sre.SRE_Match object at 0x011761A0>
## 5 None
## 6 <_sre.SRE_Match object at 0x011761A0>
## 7 None
## 8 <_sre.SRE_Match object at 0x011761A0>
## 9 <_sre.SRE_Match object at 0x011761A0>
## 10 <_sre.SRE_Match object at 0x011761A0>
## 11 None
## 12 <_sre.SRE_Match object at 0x011761A0>
## 13 None
## 14 <_sre.SRE_Match object at 0x011761A0>
## 15 <_sre.SRE_Match object at 0x011761A0>
## 16 <_sre.SRE_Match object at 0x011761A0>
## 17 None
## 18 <_sre.SRE_Match object at 0x011761A0>
## 19 None
>
--
Carsten Haesehttp://informixdb.sourceforge.net
Feb 13 '08 #3
hmm... interesting

here is another way you can find prime numbers
http://love-python.blogspot.com/2008...ms-range2.html

On Feb 13, 9:31 pm, cokofree...@gmail.com wrote:
I was reading up on this site [http://www.noulakaz.net/weblog/
2007/03/18/a-regular-expression-to-check-for-prime-numbers/] of an
interesting way to work out prime numbers using Regular Expression.

However my attempts to use this in Python keep returning none
(obviously no match), however I don't see why, I was under the
impression Python used the same RE system as Perl/Ruby and I know the
convert is producing the correct display of 1's...Any thoughts?

def re_prime(n):
import re
convert = "".join("1" for i in xrange(n))
return re.match("^1?$|^(11+?)\1+$", convert)

print re_prime(2)

Also on a side note the quickest method I have come across as yet is
the following

def prime_numbers(n):
if n < 3: return [2] if n == 2 else []
nroot = int(n ** 0.5) + 1
sieve = range(n + 1)
sieve[1] = 0
for i in xrange(2, nroot):
if sieve[i]:
sieve[i * i: n + 1: i] = [0] * ((n / i - i) + 1)
return [x for x in sieve if x]

Damn clever whoever built this (note sieve will produce a list the
size of your 'n' which is unfortunate)
Feb 13 '08 #4
On Feb 13, 5:14*pm, castiro...@gmail.com wrote:
Isn't the finite state machine "regular expression 'object'" really
large?
There's no finite state machine involved here, since this isn't a
regular expression in the strictest sense of the term---it doesn't
translate to a finite state machine, since backreferences are
involved.

Mark
Feb 13 '08 #5
On Feb 13, 5:43*pm, Mark Dickinson <dicki...@gmail.comwrote:
On Feb 13, 5:14*pm, castiro...@gmail.com wrote:
Isn't the finite state machine "regular expression 'object'" really
large?

There's no finite state machine involved here, since this isn't a
regular expression in the strictest sense of the term---it doesn't
translate to a finite state machine, since backreferences are
involved.

Mark
What is it?
Feb 14 '08 #6
hmm... interesting
>
here is another way you can find prime numbershttp://love-python.blogspot.com/2008/02/find-prime-number-upto-100-nu...
Sadly that is pretty slow though...

If you don't mind readability you can make the example I gave into
five lines.

def p(_):
if _<3:return[2]if _==2 else[]
a,b,b[1]=int(_**0.5)+1,range(_+1),0
for c in xrange(2,a):
if b[c]:b[c*c:_+1:c]=[0]*((_/c-c)+1)
return[_ for _ in b if _]

But then, I would have to kill you...
Feb 14 '08 #7
cokofree:
Sadly that is pretty slow though...
It's quadratic, and it's not even short, you can do (quadratic still):

print [x for x in range(2, 100) if all(x%i for i in range(2, x))]

In D you can write similar code.
Bye,
bearophile
Feb 14 '08 #8
On Feb 14, 5:26*am, bearophileH...@lycos.com wrote:
cokofree:
Sadly that is pretty slow though...

It's quadratic, and it's not even short, you can do (quadratic still):

print [x for x in range(2, 100) if all(x%i for i in range(2, x))]

In D you can write similar code.
Bye,
bearophile
all(x%i ha
Feb 14 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Ron Adam | last post by:
Is it possible to match a string to regular expression pattern instead of the other way around? For example, instead of finding a match within a string, I want to find out, (pass or fail), if...
8
by: Des Small | last post by:
I want to use sets and regular expressions to implement some linguistic ideas. Representing sounds by symbols, and properties (coronal or velar articulation; voicedness) by sets of symbols with...
3
by: christopher diggins | last post by:
There seems to be a gazillion regular expression libraries. Most of them only work on text, but I wanted something that also worked on arbitrary sequences of data ( this is useful, for instance, in...
2
by: Beffmans | last post by:
Hi I want to make a regularexpressionvalidator for a textbox to allow only numbers but not a specified number of digits How to do this? ch B *** Sent via Developersdex...
5
by: Trevor Braun | last post by:
Hi, I'm not sure that this is the right forum for this, but I've been having a very tough time completing this expression, and I was hoping someone might have some suggestions for me. I am trying...
3
by: Zach | last post by:
Hello, Please forgive if this is not the most appropriate newsgroup for this question. Unfortunately I didn't find a newsgroup specific to regular expressions. I have the following regular...
5
by: Cylix | last post by:
I am going to write a function that the search engine done. in search engine, we may using double quotation to specify a pharse like "I love you", How can I using regular expression to sperate...
4
by: =?Utf-8?B?ZG1idXNv?= | last post by:
I am looking for a regular expression that would filter numbers in my vb.net application. The integer part could have up to 5 digits and the fractional part up to 2 digits. I came up with the...
8
by: baskarpr | last post by:
I am new to regular expression. I have to validate an input, such as that should be non-negative numeric value (case 1) and non-negative numeric decimal value (case 2) (i.e. 0.5, 0.2, 10.0 etc) . I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.