[英]Fast Algorithm to find number of primes between two numbers
My problem reduces to finding the number of primes between two given numbers.I could have a range as big as 1 to (1000)!
我的问题减少到找到两个给定数字之间的素数。我的范围可以大到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. 是否有任何可以应用的数学优化 - 例如,占用这个大空间的较小子集并推断其余数字。
PS: 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. PS:看起来我可能已经走到了死胡同 - 但我正在寻找的是一些可能有助于解决这个问题的优化。 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. PrimeGrid项目的人们可以为此做出有益的贡献。
Since you want to go as high as 1000!
既然你想要高达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
. Prime计数功能仅针对少数10^24
的值进行了精确评估。 So no way you'll be able to hit 1000!
所以你不可能达到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 . 这是基于素数定理 ,它表示素数计数函数是对数积分的渐近 。
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. 如果您不需要精确值,那么此公式的两次评估的差异将使您接近。
The prime counting algorithm developed by Lagarias and others, quoted by others, runs very roughly in O (n^(2/3)). 由其他人引用的由Lagarias和其他人开发的素数计算算法在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. 由于从k1到k2的素数的筛子大致为O(max(sqrt(k2),k2-k1),你需要检查你的下界和上界之间的距离,并使用筛子或使用素数计算算法,哪个会更快。
BTW. 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. 对于各种值n,可以调整素数计数算法以计算从1到n的素数,它们比单独计算它们更快地合理地靠近在一起。 (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)). (基本上,它选择一个数字N,创建一个大小为n / N的筛子,并在该筛子中查找N ^ 2个值.O(n ^(2/3))来自N = n ^的事实(1/3)两个操作都需要N ^(2/3)步。这个筛可以重复用于不同的n,但是需要查找不同的值。因此对于k个不同的n值,你会使N变小一些,增加筛子的成本(仅一次)但降低查找成本(k次))。
For n around 1000!, there's no chance. n大约1000!,没有机会。 You can't even count the number of primes in [n, n] for values of that size if n has no small(ish) factors. 如果n没有小(ish)因子,你甚至不能计算[n,n]中素数的数量。
其中我知道的最快的方法是尽可能快地消除所有已知的非素数(偶数,所有数字,其中除法器的数量低于范围内的起始数等),然后迭代其余部分并使用类似欧几里得算法的东西,以确定该数字是否是素数。
You can survey your options here: http://en.wikipedia.org/wiki/Prime_counting_function 您可以在此处调查您的选项: http : //en.wikipedia.org/wiki/Prime_counting_function
This also looks helpful: http://mathworld.wolfram.com/PrimeCountingFunction.html 这看起来也很有帮助: http : //mathworld.wolfram.com/PrimeCountingFunction.html
May I inquire why you need it up to 1000! 我可以询问为什么你需要它达到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. 1-10 ^ 23有1,925,320,391,606,803,968,923个素数。 1000! 1000! = 10^120. = 10 ^ 120。 I'm curious now. 我现在很好奇。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.