f

#### 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)
```
 0
cokofreedom (157)
2/13/2008 3:31:29 PM
comp.lang.python 77058 articles. 6 followers.

13 Replies
286 Views

Similar Articles

[PageSpeed] 8

```On Wed, 2008-02-13 at 07:31 -0800, cokofreedom@gmail.com wrote:
>     return re.match("^1?\$|^(11+?)\1+\$", convert)

That needs to be either

return re.match(r"^1?\$|^(11+?)\1+\$", convert)

or

return re.match("^1?\$|^(11+?)\\1+\$", convert)

in order to prevent "\1" from being read as "\x01".

--
Carsten Haese
http://informixdb.sourceforge.net

```
 0
carsten7356 (552)
2/13/2008 3:48:46 PM
```with this works:

return re.match(r"^1?\$|^(11+?)\1+\$", convert)

but it match the non-prime numbers. So re_prime(2) will return null
and  re_prime(4) will return a match

2008/2/13, Carsten Haese <carsten@uniqsys.com>:
> On Wed, 2008-02-13 at 07:31 -0800, cokofreedom@gmail.com wrote:
> >     return re.match("^1?\$|^(11+?)\1+\$", convert)
>
> That needs to be either
>
> return re.match(r"^1?\$|^(11+?)\1+\$", convert)
>
> or
>
> return re.match("^1?\$|^(11+?)\\1+\$", convert)
>
> in order to prevent "\1" from being read as "\x01".
>
> --
> Carsten Haese
> http://informixdb.sourceforge.net
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

--
Rafael Sachetto Oliveira

Sir - Simple Image Resizer
```
 0
rsachetto (6)
2/13/2008 4:32:49 PM
```On Feb 13, 9:48=A0am, Carsten Haese <cars...@uniqsys.com> wrote:
> On Wed, 2008-02-13 at 07:31 -0800, cokofree...@gmail.com wrote:
> > =A0 =A0 return re.match("^1?\$|^(11+?)\1+\$", convert)
>
> That needs to be either
>
> return re.match(r"^1?\$|^(11+?)\1+\$", convert)
>
> or
>
> return re.match("^1?\$|^(11+?)\\1+\$", convert)
>
> in order to prevent "\1" from being read as "\x01".

But why doesn't it work when you make that change?

>
> --
> Carsten Haesehttp://informixdb.sourceforge.net

```
 0
mensanator (1210)
2/13/2008 6:40:42 PM
```> -----Original Message-----
> From: python-list-bounces+jr9445=3Datt.com@python.org [mailto:python-
> list-bounces+jr9445=3Datt.com@python.org] On Behalf Of =
mensanator@aol.com
> Sent: Wednesday, February 13, 2008 1:41 PM
> To: python-list@python.org
> Subject: Re: Regular Expression for Prime Numbers (or How I came to
> fail at them, and love the bomb)
>=20
> On Feb 13, 9:48=A0am, Carsten Haese <cars...@uniqsys.com> wrote:
> > On Wed, 2008-02-13 at 07:31 -0800, cokofree...@gmail.com wrote:
> > > =A0 =A0 return re.match("^1?\$|^(11+?)\1+\$", convert)
> >
> > That needs to be either
> >
> > return re.match(r"^1?\$|^(11+?)\1+\$", convert)
> >
> > or
> >
> > return re.match("^1?\$|^(11+?)\\1+\$", convert)
> >
> > in order to prevent "\1" from being read as "\x01".
>=20
> But why doesn't it work when you make that change?

It does work.  Read the referenced website.

If there is a match then=20
the number isn't prime
else # no match
the number is prime.

*****

The information transmitted is intended only for the person or entity to =
which it is addressed and may contain confidential, proprietary, and/or =
privileged material. Any review, retransmission, dissemination or other =
use of, or taking of any action in reliance upon this information by =
persons or entities other than the intended recipient is prohibited. If =
material from all computers. GA622

```
 0
jr9445 (82)
2/13/2008 6:48:54 PM
```On Wed, 2008-02-13 at 10:40 -0800, mensanator@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

```
 0
carsten7356 (552)
2/13/2008 6:53:00 PM
```On Feb 13, 12:53=A0pm, Carsten Haese <cars...@uniqsys.com> wrote:
> 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

```
 0
mensanator (1210)
2/13/2008 7:11:13 PM
```hmm... interesting

here is another way you can find prime numbers
http://love-python.blogspot.com/2008/02/find-prime-number-upto-100-nums-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)

```
 0
2/13/2008 7:36:00 PM
```On Feb 13, 9:48=A0am, Carsten Haese <cars...@uniqsys.com> wrote:
> On Wed, 2008-02-13 at 07:31 -0800, cokofree...@gmail.com wrote:
> > =A0 =A0 return re.match("^1?\$|^(11+?)\1+\$", convert)
>
> That needs to be either
>
> return re.match(r"^1?\$|^(11+?)\1+\$", convert)
>
> or
>
> return re.match("^1?\$|^(11+?)\\1+\$", convert)
>
> in order to prevent "\1" from being read as "\x01".
>
> --
> Carsten Haesehttp://informixdb.sourceforge.net

re.match(r"^(oo+?)\1+\$", 'o'*i ) drops i in [0,1]

and

re.match(r"^(ooo+?)\1+\$", 'o'*i ), which only drops i in [0,1,4].

Isn't the finite state machine "regular expression 'object'" really
large?
```
 0
castironpi (1589)
2/13/2008 10:14:57 PM
```On Feb 13, 5:14=A0pm, 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
```
 0
dickinsm (350)
2/13/2008 11:43:20 PM
```On Feb 13, 5:43=A0pm, Mark Dickinson <dicki...@gmail.com> wrote:
> On Feb 13, 5:14=A0pm, 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?
```
 0
castironpi (1589)
2/14/2008 2:05:47 AM
```> 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...
```
 0
cokofreedom (157)
2/14/2008 8:33:05 AM
```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
```
 0
2/14/2008 11:26:24 AM
```On Feb 14, 5:26=A0am, 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
```
 0
castironpi (1589)
2/14/2008 5:35:23 PM