473,732 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Ultimate Prime Sieve -- Sieve Of Zakiya (SoZ)

This is to announce the release of my paper "Ultimate Prime Sieve --
Sieve of Zakiiya (SoZ)" in which I show and explain the development of
a class of Number Theory Sieves to generate prime numbers. I used
Ruby 1.9.0-1 as my development environment on a P4 2.8 Ghz laptop.

You can get the pdf of my paper and Ruby and Python source from here:

http://www.4shared.com/dir/7467736/9...1/sharing.html

Below is a sample of one of the simple prime generators. I did a
Python version of this in my paper (see Python source too). The Ruby
version below is the minimum array size version, while the Python has
array of size N (I made no attempt to optimize its implementation,
it's to show the method).

class Integer
def primesP3a
# all prime candidates 3 are of form 6*k+1 and 6*k+5
# initialize sieve array with only these candidate values
# where sieve contains the odd integers representatives
# convert integers to array indices/vals by i = (n-3)>>1 =
(n>>1)-1
n1, n2 = -1, 1; lndx= (self-1) >>1; sieve = []
while n2 < lndx
n1 +=3; n2 += 3; sieve[n1] = n1; sieve[n2] = n2
end
#now initialize sieve array with (odd) primes < 6, resize array
sieve[0] =0; sieve[1]=1; sieve=sieve[0..lndx-1]

5.step(Math.sqr t(self).to_i, 2) do |i|
next unless sieve[(i>>1) - 1]
# p5= 5*i, k = 6*i, p7 = 7*i
# p1 = (5*i-3)>>1; p2 = (7*i-3)>>1; k = (6*i)>>1
i6 = 6*i; p1 = (i6-i-3)>>1; p2 = (i6+i-3)>>1; k = i6>>1
while p1 < lndx
sieve[p1] = nil; sieve[p2] = nil; p1 += k; p2 += k
end
end
return [2] if self < 3
[2]+([nil]+sieve).compact !.map {|i| (i<<1) +3 }
end
end

def primesP3(val):
# all prime candidates 3 are of form 6*k+(1,5)
# initialize sieve array with only these candidate values
n1, n2 = 1, 5
sieve = [False]*(val+6)
while n2 < val:
n1 += 6; n2 += 6; sieve[n1] = n1; sieve[n2] = n2
# now load sieve with seed primes 3 < pi < 6, in this case just 5
sieve[5] = 5

for i in range( 5, int(ceil(sqrt(v al))), 2) :
if not sieve[i]: continue
# p1= 5*i, k = 6*i, p2 = 7*i,
p1 = 5*i; k = p1+i; p2 = k+i
while p2 <= val:
sieve[p1] = False; sieve[p2] = False; p1 += k; p2 += k
if p1 <= val: sieve[p1] = False

primes = [2,3]
if val < 3 : return [2]
primes.extend( i for i in range(5, val+(val&1), 2) if sieve[i] )

return primes

Now to generate an array of the primes up to some N just do:

Ruby: 10000001.primes P3a
Python: primesP3a(10000 001)

The paper presents benchmarks with Ruby 1.9.0-1 (YARV). I would love
to see my various prime generators benchmarked with optimized
implementations in other languages. I'm hoping Python gurus will do
better than I, though the methodology is very very simple, since all I
do is additions, multiplications , and array reads/writes.

Have fun with the code. ;-)

Jabari Zakiya
Jun 27 '08 #1
3 1652
On Jun 13, 1:12*pm, jzakiya <jzak...@gmail. comwrote:
This is to announce the release of my paper "Ultimate Prime Sieve --
Sieve of Zakiiya (SoZ)" in which I show and explain the development of
a class of Number Theory Sieves to generate prime numbers. * I used
Ruby 1.9.0-1 as my development environment on a P4 2.8 Ghz laptop.

You can get the pdf of my paper and Ruby and Python source from here:

http://www.4shared.com/dir/7467736/9...1/sharing.html

Below is a sample of one of the simple prime generators. I did a
Python version of this in my paper (see Python source too). *The Ruby
version below is the minimum array size version, while the Python has
array of size N (I made no attempt to optimize its implementation,
it's to show the method).

class Integer
* *def primesP3a
* * * # all prime candidates 3 are of form *6*k+1 and 6*k+5
* * * # initialize sieve array with only these candidate values
* * * # where sieve contains the odd integers representatives
* * * # convert integers to array indices/vals by *i = (n-3)>>1 =
(n>>1)-1
* * * n1, n2 = -1, 1; *lndx= (self-1) >>1; *sieve = []
* * * while n2 < lndx
* * * * *n1 +=3; * n2 += 3; * sieve[n1] = n1; *sieve[n2] = n2
* * * end
* * * #now initialize sieve array with (odd) primes < 6, resize array
* * * sieve[0] =0; *sieve[1]=1; *sieve=sieve[0..lndx-1]

* * * 5.step(Math.sqr t(self).to_i, 2) do |i|
* * * * *next unless sieve[(i>>1) - 1]
* * * * *# p5= 5*i, *k = 6*i, *p7 = 7*i
* * * * *# p1 = (5*i-3)>>1; *p2 = (7*i-3)>>1; *k = (6*i)>>1
* * * * *i6 = 6*i; *p1 = (i6-i-3)>>1; *p2 = (i6+i-3)>>1; *k = i6>>1
* * * * *while p1 < lndx
* * * * * * *sieve[p1] = nil; *sieve[p2] = nil; *p1 += k; *p2 += k
* * * * *end
* * * end
* * * return [2] if self < 3
* * * [2]+([nil]+sieve).compact !.map {|i| (i<<1) +3 }
* *end
end

def primesP3(val):
* * # all prime candidates 3 are of form *6*k+(1,5)
* * # initialize sieve array with only these candidate values
* * n1, n2 = 1, 5
* * sieve = [False]*(val+6)
* * while *n2 < val:
* * * * n1 += 6; * n2 += 6; *sieve[n1] = n1; * sieve[n2] = n2
* * # now load sieve with seed primes 3 < pi < 6, in this case just 5
* * sieve[5] = 5

* * for i in range( 5, int(ceil(sqrt(v al))), 2) :
* * * *if not sieve[i]: *continue
* * * *# *p1= 5*i, *k = 6*i, *p2 = 7*i,
* * * *p1 = 5*i; *k = p1+i; *p2 = k+i
* * * *while p2 <= val:
* * * * * sieve[p1] = False; *sieve[p2] = False; *p1 += k; *p2 += k
* * * *if p1 <= val: *sieve[p1] = False

* * primes = [2,3]
* * if val < 3 : return [2]
* * primes.extend( i for i in range(5, val+(val&1), 2) *if sieve[i] )

* * return primes

Now to generate an array of the primes up to some N just do:

Ruby: * *10000001.prime sP3a
Python: primesP3a(10000 001)

The paper presents benchmarks with Ruby 1.9.0-1 (YARV). *I would love
to see my various prime generators benchmarked with optimized
implementations in other languages. *I'm hoping Python gurus will do
better than I, though the methodology is very very simple, since all I
do is additions, multiplications , and array reads/writes.

Have fun with the code. *;-)
CORRECTION:

http://cr.yp.to/primegen.html NOT "primesgen"

Jabari Zakiya

Jun 27 '08 #2
On Jun 13, 1:12 pm, jzakiya <jzak...@gmail. comwrote:
The paper presents benchmarks with Ruby 1.9.0-1 (YARV). I would love
to see my various prime generators benchmarked with optimized
implementations in other languages. I'm hoping Python gurus will do
better than I, though the methodology is very very simple, since all I
do is additions, multiplications , and array reads/writes.
After playing a little with it, I managed to get a 32-47% improvement
on average for the pure Python version, and a 230-650% improvement
with an extra "import psyco; psyco.full()" (pasted at http://codepad.org/C2nQ8syr)
The changes are:

- Replaced range() with xrange()
- Replaced x**2 with x*x
- Replaced (a,b) = (c,d) with a=c; b=d
- Replaced generator expressions with list comprehensions. This was
the big one for letting psyco do its magic.

I also tried adding type declarations and running it through Cython
but the improvement was much less impressive than Psyco. I'm not a
Pyrex/Cython expert though so I may have missed something obvious.

George
Jun 27 '08 #3
On Jun 18, 7:58*pm, George Sakkis <george.sak...@ gmail.comwrote:
On Jun 13, 1:12 pm, jzakiya <jzak...@gmail. comwrote:
The paper presents benchmarks with Ruby 1.9.0-1 (YARV). *I would love
to see my variousprimegen erators benchmarked with optimized
implementations in other languages. *I'm hoping Python gurus will do
better than I, though the methodology is very very simple, since all I
do is additions, multiplications , and array reads/writes.

After playing a little with it, I managed to get a 32-47% improvement
on average for the pure Python version, and a 230-650% improvement
with an extra "import psyco; psyco.full()" (pasted athttp://codepad.org/C2nQ8syr)
The changes are:

- Replaced range() with xrange()
- Replaced x**2 with x*x
- Replaced (a,b) = (c,d) with a=c; b=d
- Replaced generator expressions with list comprehensions. This was
the big one for letting psyco do its magic.

I also tried adding type declarations and running it through Cython
but the improvement was much less impressive than Psyco. I'm not a
Pyrex/Cython expert though so I may have missed something obvious.

George
George,

I took your code and included more efficient/optimized versions of
SoZ versions P3, P5, P7, and P11.

I ran the code on my PCLinuxOS, Intel P4, Python 2.4.3 system and
noted this. The SoZ code run much faster than the SoA in pure Python.
When psyco is used the SoA is significantly faster than the
pure Python version. The SoZ versions are faster too, but now
they are slower than the SoA. You can download the code from
http://www.4shared.com/dir/7467736/9...1/sharing.html

It would be interesting to see how this code runs in newer versions
of Python (Psyco).

FYI, someone else coded P7 in C on a QuadCore Intel 9650 3.67GHz
overclocked cpu, using multiple threads, and got it to be faster
than the SoA, SoE, Here's some of his results (times in seconds).

Case nPrime7x nPrime7x nPrime7x nPrime7x
Atkin Zakiya Eratosthenes Zakiya (8 core
2.5ghz)

100 billion 52.58 44.27 50.56

200 b 110.14 92.38 108.99 88.01

300 b 169.81 140.92 167.47

400 b 232.34 190.84 228.08 177.72

500 b 297.44 241.84 291.28

600 b 364.84 293.92 355.27 273.04

700 b 434.33 346.97 420.41

800 b 506.67 400.97 486.72 373.29

900 b 579.58 456.53 555.09

1 trillion 654.03 513.11 624.00 479.22
Jabari
Jul 19 '08 #4

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

Similar topics

11
5940
by: lostinpython | last post by:
I'm having trouble writing a program that figures out a prime number. Does anyone have an idea on how to write it? All I know is that n > 2 is prim if no number between 2 and sqrt of n (inclusivly) evenly divides n.
32
76439
by: Cmorriskuerten | last post by:
HI, is this is this solution to test if a number is a prime number or not: /* * Is n a prime number? * Return TRUE (1): n is a prime number * Return FALSE (0): n is a *not* a prime number */ int is_prime_number(int n)
19
7493
by: swisscheese | last post by:
I figured someone out there must have written a minimal code size prime number generator. I did not find one after a bit of searching around. For primes up to 100 the best I could do was 70 characters (including spaces): r=range(2,99) m=
10
4435
by: Joel Mayes | last post by:
Hi All; I'm teaching myself C, and have written a prime number generator. It is a pretty inefficient implementation of the Sieve of Eratosthenes to calculate primes up to 1,000,000. If anyone has time to critic and offer my some feedback I'd be grateful Thanks Joel
60
1950
by: rhle.freak | last post by:
Here is my code to generate prime numbers.It works absolutely fine when the range is *not very large*. However on initializing i with a large integer it produces erroneous results (some numbers ending in 5 ..which obviously cannot be prime numbers) can anyone please help me out with the reason?? /*Generate Prime Numbers*/ #include<stdio.h>
2
10597
Blackout
by: Blackout | last post by:
Hi, I'm having problems with this C program. Whenever I run it, it doesn't print anything. The program is supposed to compute and display all the prime numbers from 1 - 300 using the sieve of eratosthenes and using arrays (passing arrays to functions). Whenever i run it the compiler compiles it, shows a window for half a second, and closes again. Here is what I've done so far: /* This program uses the Sieve of Eratosthenes to * find all the...
8
2777
by: cokofreedom | last post by:
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...
5
2277
by: jzakiya | last post by:
This is to announce the release of my paper "Ultimate Prime Sieve -- Sieve of Zakiiya (SoZ)" in which I show and explain the development of a class of Number Theory Sieves to generate prime numbers. I used Ruby 1.9.0-1 as my development environment on a P4 2.8 Ghz laptop. You can get the pdf of my paper and Ruby and Python source from here: http://www.4shared.com/dir/7467736/97bd7b71/sharing.html Below is a sample of one of the...
4
2711
by: jzakiya | last post by:
Update: 2008/11/03 Architecture & coding improvements. Renamed generators. I am 90% finished writing up a mathematical analysis of my method. In the process I found an architectural optimization to the sieve process which is incorporated in the new code. Complexity analysis showing other interesting stuff for each generator. When I finish I will post paper here with the code:
0
8944
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8773
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9306
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9234
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9180
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6030
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4805
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.