What is the Fastest Method for Finding Prime Numbers and Combinations?

  • Thread starter Superposed_Cat
  • Start date
  • Tags
    Prime
In summary: The Miller-Rabin test for primality of a given tests for exactly what it says, but it is probabalistic.The sieve of Eratosthenes is an algorithm that can find prime numbers. It is slow, but it is the fastest I've found. There are many algorithms to find all possible combinations of a set.
  • #1
Superposed_Cat
388
5
Hi, I was wondering what A)the fastest way to find primes is, the fastest I've found so far is the sieve of Eratosthenes.
B) The fastest way to find all possible combinations of a set are. e.g. cat-> act,cta,tca,atc,tac,cat
any help appreciated. thanks in advance.
 
Last edited:
Technology news on Phys.org
  • #2
Superposed_Cat said:
Hi, I was wondering what A)the fastest way to find primes is, the fastest I've found so far is the sieve of Eratosthenes.
The fastest way to find n arbitrary primes? Then this sieve is not bad.
The fastest way to check if a number is prime? Then the sieve is really bad apart from the first ~100 numbers (where you don't need it anyway).

B) The fastest way to find all possible combinations of a set are. e.g. cat-> act,cta,tca,atc,tac,cat
any help appreciated. thanks in advance.
Use some systematic way to list all permutations. There are many algorithms to do that.
 
  • #3
Superposed_Cat said:
B) The fastest way to find all possible combinations of a set are. e.g. cat-> act,cta,tca,atc,tac,cat
any help appreciated. thanks in advance.

Pick the first item at random. Then append the number of combinations of the smaller set that remains.

(Actually, this is a good computer programming exercise in writing recursive functions, and it's not quite as simple as it might seem!)
 
  • #4
mfb said:
Then the sieve is really bad apart from the first ~100 numbers (where you don't need it anyway).
How would you find the nth prime?

mfb said:
Use some systematic way to list all permutations. There are many algorithms to do that.
I need one that could solve for all permutations for a 30 element set in under 3 hours.
 
  • #5
Superposed_Cat said:
How would you find the nth prime?
Please don't remove relevant context in quotes:
mfb said:
The fastest way to check if a number is prime? Then the sieve is really bad apart from the first ~100 numbers (where you don't need it anyway).
To find the nth prime if n is not too large, I would first estimate its magnitude via the approximate prime density. Then use the sieve up to some number, afterwards I guess trial division is better due to its reduced memory requirements.

I need one that could solve for all permutations for a 30 element set in under 3 hours.
There are 30! or roughly 2*1032 of those permutations. Even if you can use every computer worldwide, there is no way to list or even store all those permutations within 3 hours.
Finding the next permutation is cheap in terms of CPU cycles, but the number of CPU cycles has some limit...
 
  • Like
Likes 1 person
  • #6
Oh.. last year in an Olympiad we were asked for the number of possible permutations of a 30 element set. We (it was a group thing) assumed that you would have to list the permutations first, I didn't think to just find the factorial. I feel so stupid now.
 
  • #7
Aww poor guys. You're still busy writing them out, I presume?
 
  • #8
If you can convert the whole mass of Earth to writing paper of 80g/m^2, you have 0.3mm^2 for each permutation or 0.01mm^2 per element. Letters with a size of 100µmx100µm are hard to read, but I guess that's not the most pressing issue then.

Where does the prime number question come from?
 
  • #9
There is an annual olympiad and there is always a question that has something to do with the 100000th prime number or something and another about permutations, you have 3 hours to answer 10 questions. So you can see why I'm trying to find more efficient techniques. Each question has an integer answer and will take along time to work out if your code isn't efficient.
 
  • #10
Superposed_Cat said:
There is an annual olympiad and there is always a question that has something to do with the 100000th prime number

Chances are you don't have to know this number to answer the question.
 
  • #11
ja, we thought too literal, now I know to think out of the box. the winning team only got 6/10 questions right and the people who worked for the place only got 7/10 right we got 4/10.
 
  • #12
30! = 265 252 859 812 191 058 636 308 480 000 000

By hand, this takes about 10 minutes to calculate. First get the number of zeroes at the end, and then get the rest. I found it convenient to do this in two pieces: take out the factors of 2, multiply the rest together, and then multiply this by all the factors of 2.
 
  • #13
10 minutes sounds quick. Took me 6 minutes (that's what the watch says, didn't feel like that) to reduce it to 29 * 23 * 19 * 17 * 49 * 1001^2 * 2187^2 * 524288 * 10^7. After that, it is just stupid multiplication with big numbers, but with a group this is easy to parallelize.
 
  • #14
The Miller-Rabin test for primality of a given tests for exactly what it says, but it is probabalistic.
http://en.wikipedia.org/wiki/Miller–Rabin_primality_test#Algorithm_and_running_time

Polynomial sieves: http://en.wikipedia.org/wiki/Quadratic_sieve are fast factorization algorithms, and are very fast for integers less than 100 decimal digits.

For finding the 100000th prime the Sieve of Eratosthenes is more than adequate. Just ran on under cygwin on an intel 2.8GHz dual-core. Got the answer in less than 8+ milliseconds. Actually it found the first 1,000,000 primes. But I did not feel like recoding it to make it stop at 100,000.

If this kind of number theory coding is interesting for you I would recommend Project Euler:
http://projecteuler.net/
 
  • Like
Likes 1 person
  • #15
+1 for Project Euler, I'm a fan!
 
  • #16
Same. Nice profile pic kreil. Is that you?
 
  • #17
mfb said:
10 minutes sounds quick. Took me 6 minutes (that's what the watch says, didn't feel like that) to reduce it to 29 * 23 * 19 * 17 * 49 * 1001^2 * 2187^2 * 524288 * 10^7. After that, it is just stupid multiplication with big numbers, but with a group this is easy to parallelize.

30! = 215+7+3+1310+3+156+17411213217*19*23*29=226314577411213217*19*23*29

Basically, for a given prime p<n, then at least floor(n/p) numbers are going to have p as a factor. Since they are multiplied, pfloor(n/p) is a factor. But then of those, an every p-th one of those numbers has an additional power of p. So essentially:
[itex]
\prod_{p}{p^{\sum_{k=1}^{\lfloor log_{p}(n)\rfloor}{\lfloor\frac{n}{p^{k}}}\rfloor}}[/itex]
Where p is over all primes. Alternatively, the sum can go from 1 to infinity and still be correct, but the value I provided is the last non-trivial value of the sum (the rest become 0).

Hopefully I got that all correct as I pulled it from my brain and that is known to not be so lucid at these hours :P

Depending on memory constraints and available math commands, you could determine the 100000th prime by keeping track of each new prime in an array and it's inverse in another array. Then you can multiply X (input) by each element in the inverse array up to the nth element, and once floor(sqrt(x))>array[n], increment n. You don't ever need to calculate sqrt(x), either ! Just use two counters C and D where D gets incremented by 2 every time C is reset to D.
 
  • Like
Likes 1 person
  • #18
Superposed_Cat said:
Same. Nice profile pic kreil. Is that you?

Nope, just a big Seinfeld fan
 

What is a "Fastest Prime Number Sieve"?

A "Fastest Prime Number Sieve" is a mathematical algorithm used to efficiently find prime numbers up to a certain limit. It is a method of sieving out composite numbers and leaving only prime numbers behind.

Why is finding prime numbers important?

Prime numbers are important in many areas of mathematics, including number theory, cryptography, and computer science. They are also used in many real-life applications, such as generating secure passwords and encoding data.

How does a "Fastest Prime Number Sieve" work?

A "Fastest Prime Number Sieve" works by dividing a given range of numbers into smaller intervals and using a series of sieves to identify which numbers are prime. The sieves eliminate multiples of previously identified primes, leaving only the prime numbers behind.

What makes a "Fastest Prime Number Sieve" efficient?

The efficiency of a "Fastest Prime Number Sieve" depends on its ability to quickly identify and eliminate composite numbers. This is achieved through the use of advanced algorithms and data structures, such as the segmented sieve and the wheel factorization method.

Are there limitations to a "Fastest Prime Number Sieve"?

While a "Fastest Prime Number Sieve" is a highly efficient method of finding prime numbers, it is not without its limitations. As the limit of the desired prime numbers increases, the time and space complexity of the sieve also increase. Additionally, the sieve may not work for extremely large numbers due to memory limitations.

Similar threads

  • Programming and Computer Science
Replies
14
Views
2K
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
32
Views
3K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
4
Views
10K
  • Programming and Computer Science
Replies
1
Views
955
  • Other Physics Topics
Replies
12
Views
6K
  • Programming and Computer Science
Replies
1
Views
2K
  • Linear and Abstract Algebra
Replies
6
Views
3K
Replies
2
Views
2K
Back
Top