Fast algorithm for finding number of p less than n

  • Thread starter heartless
  • Start date
  • Tags
    Algorithm
In summary: If the prime index is greater than the number of bytes in the array, then the prime is not in the array and you should stop. You can then go back to Step 2 and start over.You're doing it wrong! Start by clearing the whole array and then setting it to 1 again. Then you can just go through the entire array, setting it to a 1 as you go.In summary, your algorithm is not correct. You should start by clearing the array and then setting it to 1, and then going through the entire array.
  • #1
heartless
220
2
Hello,
I'm looking for some good and fast algorithm to find all prime numbers less than a given number. I've got a simple program which finds all primes less than any number, but once n gets above 500,000 it takes much too long. (More than 5 minutes. Just up to 100000 it takes about 7 seconds.)
My method is simply sieve of Eratosthenes, divide each number by every consecutive and so on. What'd be your method?
Thanks,
 
Computer science news on Phys.org
  • #2
There are very fast primality tests. Start with wikipedia's page to see the general picture, then you can follow the links to descriptions of the algorithms, or [much easier] working implementations.

http://en.wikipedia.org/wiki/Primality_test
 
  • #3
heartless said:
My method is simply sieve of Eratosthenes, divide each number by every consecutive and so on.
That's not the right way to do the sieve!

You shouldn't sieve by every number... only the primes! Remember how you did it by hand: you started off by crossing off every multiple of 2, then you saw 3 was prime, and crossed off every multiple of 3.

Did you then cross off every multiple of 4? No! You went straight to 5, because that's the next prime!



There are better sieves, but more complicated, and this one should be more than enough for your purposes.


Rach3 said:
There are very fast primality tests.
That's not the way you want to do it; primality testing is only good if you have a few numbers you want to test. Sieving is better, because it works on all the numbers simultaneously.
 
Last edited:
  • #4
The newest method I just knew -well, maybe a little out-of-date to most of you- is to use Pythagore

]edit[ it's "gore" not "gon" :biggrin:
 
Last edited:
  • #5
hehe...I set myself the same damn problem last weekend :biggrin: ...for me things get slow at 20,000 but my algorithm so far is the ultimate in basic...what I am trying so far though is to get some code to work properly where it only checks for divisibility by numbers less than sqrt(n) (if a number p higher than n's square root divides n then the result would have been a number less than p that divides n anyway..so continuing is pointless)...damn thing isn't working yet though :grumpy: ...once I've done this I'm going to figure out how to make it start at n = 1, check every n and throws whatever primes it finds into an array and only uses these to divide all subsequent n...ie; the sieve (I know vaguely what I have to do but I don't quite have the knack yet of making my computer do as its told without acting silly.)
 
Last edited:
  • #6
That's probably because you're using delphi or pascal, dunno.
Thanks guys for help, I'll code using the given algorithm.
Hurkyl, that was a shortened explanation of sieve :P
 
  • #7
check every n and throws whatever primes it finds into an array and only uses these to divide all subsequent n...ie; the sieve
If I understand you correctly, that's not a sieve. In fact, that's the very thing a sieve is designed to avoid! If you're doing a sieve1 to find all the primes less than some bound, you will never do any divisions at all!

The whole point of a sieve is that you are not doing a bunch of work for one number, then a bunch of work for the next number, and so forth... but instead are working with the entire collection of numbers at once.

Wikipedia has a nice animation of how the sieve of Eratosthenes works.



1: at least, if you're doing the simplest sort of sieve. More complicated sieves might require some divisions for the setup, but not for the actual sieving.
 
Last edited:
  • #8
ouch! :redface: I stand corrected :smile:...not that it will do me any good but what I meant was say we're lookiing at n = 5, then using p=2 and the fact that the next prime stored... 3 is greater than sqrt(5) store 5 as a prime then look at 6, and dis-regard it, look at 7 and again only use 2 and store it as a prime...disregard 8,9,10, use only 2 and 3 to divide 11 before storing it as a prime and so on... pulling all multiples of the primes (up to a certain number) out of a* look at these numbers* box is something I will give more thought
 
Last edited:
  • #9
I didn't see any full explanations of the sieve method.

Code:
Start off with an array of n bytes
Set all bytes to a 1.

Set the prime index to 2.

while ((prime index squared) < n)
   Set the array index to the prime index squared.
   While(array index < n)
      clear byte at array index
      add prime index to array index
   endwhile

   starting at prime index +1, scan for next byte in array == 1
   (note Intel / AMD processors have an assembly instruction,
    repne scasb (AL==1), that can speed this up greatly,
    else do this via a loop).
   set new prime index to the index of the next byte == 1
endwhile

Rescan array from 2 to n-1, the indexes of all bytes == 1 are primes.
 
Last edited:

1. How does the fast algorithm for finding the number of p less than n work?

The fast algorithm for finding the number of p less than n is based on the Sieve of Eratosthenes. It works by creating a list of numbers from 2 to n and eliminating all the multiples of each prime number in the list. The remaining numbers are all prime numbers less than n, and the size of the list gives the total number of prime numbers less than n.

2. What makes this algorithm faster than other methods?

The fast algorithm for finding the number of p less than n is faster because it only needs to go through the list of numbers once, eliminating multiples as it goes. Other methods, such as checking each number individually for primality, require more computations and are therefore slower.

3. Can this algorithm be used for finding the number of prime numbers in a large range?

Yes, this algorithm can be used for finding the number of prime numbers in a large range. However, for extremely large ranges, the algorithm may become less efficient due to the memory and processing power required to store and manipulate the list of numbers.

4. Is the fast algorithm for finding the number of p less than n always accurate?

Yes, the algorithm is always accurate as long as the input n is within the limits of the data type being used. For example, if n is larger than the maximum value that can be stored in an integer, the algorithm may not give accurate results.

5. Are there any limitations to using this algorithm?

One limitation of this algorithm is that it requires a list of numbers from 2 to n to be stored in memory. This can become problematic for very large values of n, as it may require a large amount of memory and processing power. Additionally, the algorithm may not be efficient for finding the number of prime numbers in a small range, as the overhead of creating and manipulating the list of numbers may outweigh the benefits of the algorithm.

Similar threads

  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
22
Views
750
Replies
1
Views
2K
  • Programming and Computer Science
Replies
30
Views
4K
Replies
9
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
33
Views
3K
  • Quantum Physics
Replies
3
Views
946
  • Engineering and Comp Sci Homework Help
Replies
32
Views
3K
  • General Math
Replies
13
Views
1K
Back
Top