Generateing all primes from 2-N the fast way

Started by
17 comments, last by Specchum 19 years, 1 month ago
Erathostenes comes to mind.

Make an array of bools of length n(n largest prime).
set all entries to true.
Just make a for loop from 2 until sqrt(n).
Now in the loop. 2 comes first.

if(array)
{
//set all multiples of i to false
}


Thats it very effective.

-CProgrammer
Advertisement
Nice, so you don't have to do any primality checking on those numbers?

Sweet, now i'm 30 seconds for 20mill.
2 min 53 secons for 100M

But how can i use it for much larger numbers...

Now, i am trying to sieve up to about 3.2GB (using 200MB of ram, how much memory should i be able to use? i have 256mb of ram, win2K).

I was thinking about 1. Using bit tables (how slow are they?) and 2. by only storing every second number. So i use half the space. (so then i can use 2*8*200M = 3.2G prime numbers).

I would need to search 1,094,967,296 numbers using trial division later.
That would take ages to do......

Now to search the whole thing, i would need 268,435,456 or 268MB, i only have 256mb... I'm going to see if i can find a comp with more memory...

How would i use the bitfields and the using every second number thing? (keep in mind, that i'm using vb, so bitshifts = mults).

Am i going quickly, given programming language and experience?

From,
Nice coder

[Edited by - Nice Coder on March 5, 2005 4:38:38 AM]
Click here to patch the mozilla IDN exploit, or click Here then type in Network.enableidn and set its value to false. Restart the browser for the patches to work.
I've been thinking of making a little test program, so what it would do, is it would sieve up to x where x is a user specified number, before going and resolving queries.

Queries would be like: What is the closest prime number to x, is x prime, how many primes do you have, how fast did you search, what is a random prime number, multply x prime numbers together and give me the result, ect.

From,
Nice coder
Click here to patch the mozilla IDN exploit, or click Here then type in Network.enableidn and set its value to false. Restart the browser for the patches to work.
I've been thinking about something like dynamic programming.
If you're generating primes from 2 until a certain number, your primality testing can be different and much more efficient then "the usual way".
That is because for every number, you already have generated all the primes smaller then it.

I thought about something like that (algorithm):

primes = {2}
currentNum = 3

while currentNum

eventually, the set "primes" will contain all the prime numbers smaller or equal to n.
The set "primes" can be implemented as a vector or a list. It does not really matter.


There is another, much simpler algorithm, but it uses more memory.
You make a big boolean vector V of size n-1. The n'th element represents the number n+2. That is V[0] represents 2, V[1] represents 3 and so on.
All the values in the vector are initialized to be false.
For index i in the vector, if V is false then you make all the values at indices i, 2i, 3i,... to be true.
Eventually, the array is scanned sequentially, and all indices of values which remained false represent prime numbers (they are not multiples of any number). This algorithm can be improved too to work faster.
Thats the sieve.

I'm trying to figure out how to use bittables, so t hat i can work with individual bits quickly....

Has anybody got any experience in this?

From,
Nice coder
Click here to patch the mozilla IDN exploit, or click Here then type in Network.enableidn and set its value to false. Restart the browser for the patches to work.
Actually thats the type of thing that i tried. (really slow).

All that was needed was

open "C:\Outputfile.txt" for output as #1dim primes() as booleanredim primes(number)for x = 3 to numberif primes(x) = false then         print #1, x         for y = x to number step x         primes(y) = true         next yend ifnext xclose #1


Which searchesa bit over 1/2 million nums/second on my machine (p31ghz, 256mb ram).
Without needing to print off the data, it searches about 3/4-1 million nums/second.

Now, the thing that i'm a bit clueless about, is using bittables....

From,
Nice coder
Click here to patch the mozilla IDN exploit, or click Here then type in Network.enableidn and set its value to false. Restart the browser for the patches to work.
If you're worried about speed then why don't you consider switching away from VB? (sorry I can't be more helpful...[smile]) (p.s. using the paging algorithm from the page that fruny linked to I just generated primes to over 200 million...so now I have a bunch of useless files full of primes sitting on my hard drive...stupid impulses)

[Edited by - lucky_monkey on March 6, 2005 8:33:19 AM]
I would get away from vb, but then again, i want a fast algorithm... (i also have a hate-hate relationship with my c++ compiler.) Also, wheres the challange of programming something fast in a language which has no optimisation and does everything the slow way? where a division can cost a few hundred times what it should cost, when every instruction needs to be carefully hand optimised....

Maybe i could make all k primes to 2^32? k being 101? (after i check the first few billion numbers, using a sieve that i'm still looking forward to building. As well as being completely stumed in how to build it...)

There would only be a very small chance that the numbers that are k prime over 101 are actually compisite.

I'm making a smallish adjustment to the sieve:
I'm going to make a sieve up to 200 million (assuming that nobody can tell me how to implement the changes, and i cannot figure out myself)
After that i go into a kprime mode where i use an isprime of the form
dim j as doublej = x * 0.5j += x * 0.3333333333333333333j += x * 0.2....isprime = (int(j) <> j)


Is this a nice way to do it?
From,
Nice coder
Click here to patch the mozilla IDN exploit, or click Here then type in Network.enableidn and set its value to false. Restart the browser for the patches to work.
Quote:Original post by Telastyn
There was a recent breakthrough in India [?] regarding primality testing, which might help.


The new algorithm was developed by a team of researchers from Indian Institure of Technology. It is, however, an "accurate primality" algorithm rather than a speedy algorithm. Apparently, it generates primes with NO errors and is more oriented towards encryption and security paradigms.
"There is no dark side of the moon." - Pink Floyd

This topic is closed to new replies.

Advertisement