Fast Algorithm to find number of primes between two numbers

大城市里の小女人 提交于 2019-12-03 10:38:46

问题


My problem reduces to finding the number of primes between two given numbers.I could have a range as big as 1 to (1000)! and hence I am need of some mathematical optimizations.

Clearly the sieve method would be too slow in this case. Is there any mathematical optimization that can be applied - like for instance, taking a smaller subset of this large space and making inferences about the rest of the numbers.

P.S: It definitely looks like I might have reached a dead end - but all that I am looking for are some optimizations that could possibly help solve this. And also, I am only looking for a single-threaded approach.

EDIT: One approach I have been thinking and can solve a lot of large prime number related problems - is for someone to maintain a global table of primes and make it available for lookup. Folks at the PrimeGrid project can contribute usefully towards this.


回答1:


Since you want to go as high as 1000! (factorial). You will not be able to get exact results with currently known methods on current technology.

The Prime Counting Function has only been evaluated exactly for a handful of values up to 10^24. So no way you'll be able to hit 1000!.


But since you mention than an approximation may be fine, you can use the Logarithmic Integral as an approximation to the Prime Counting Function.

This is based on the Prime Number Theorem which says that the Prime Counting Function is asymptotic to the Logarithmic Integral.




回答2:


There is a fast, simple approximation to the number of primes below a given bound. If you do not need exact values, then a difference of two evaluations of this formula will get you close.




回答3:


The fastest method that I know would be to eliminate all of the known non-primes (even numbers, all the numbers with divisers lower than the start number in the range, etc) as fast as you can, then iterate over the rest and use something like the Euclidean algorithm to determine if that number is a prime.




回答4:


You can survey your options here: http://en.wikipedia.org/wiki/Prime_counting_function

This also looks helpful: http://mathworld.wolfram.com/PrimeCountingFunction.html

May I inquire why you need it up to 1000! ? Looks like no one has ever counted that many before. There are 1,925,320,391,606,803,968,923 primes from 1-10^23. 1000! = 10^120. I'm curious now.




回答5:


The prime counting algorithm developed by Lagarias and others, quoted by others, runs very roughly in O (n^(2/3)). Since a sieve for the primes from k1 to k2 takes roughly O (max (sqrt (k2), k2 - k1), you'd check how far your lower and upper bounds are apart and either do a sieve or use the prime counting algorithm, whichever will be faster.

BTW. The prime counting algorithm can be tuned to count the primes from 1 to n for various values n that are reasonably close together quicker than counting them individually. (Basically, it chooses a number N, creates a sieve of size n / N, and looks up N^2 values in that sieve. The O (n^(2/3)) comes from the fact that for N = n^(1/3) both operations take N^(2/3) steps. That sieve can be reused for different n, but different values need to be looked up. So for k different values of n, you make N a bit smaller, increasing the cost of the sieve (once only) but reducing the cost of the lookup (k times)).

For n around 1000!, there's no chance. You can't even count the number of primes in [n, n] for values of that size if n has no small(ish) factors.



来源:https://stackoverflow.com/questions/8936552/fast-algorithm-to-find-number-of-primes-between-two-numbers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!