【发布时间】:2021-02-07 16:55:54
【问题描述】:
Complex plot of the Analytic version
在所有 Covid 停机期间,我一直在做一些数论,我认为我发现了一种非常有趣(如果不是新颖的话)的算法来检测素数。我在我的 LinkedIn 页面上发布了我的文章,您无需注册或其他任何东西。
https://www.linkedin.com/pulse/efficient-prime-number-generation-christopher-wolfe/
我只想知道这种技术是否已经为人所知,因为它非常快(恒定时间)并且尺寸非常紧凑。不久前我进行了更深入的研究,您可以在我的博客上阅读。
http://jasuto.com/ideas/primes/
很高兴获得一些反馈并验证这是新的。我还有很多东西要发布,但我想我会开始轻松:)
如果您不想阅读本文,这里是演示代码。我见过很多素数的实现,但没有 O(1) 或这么小的......
# Copyright 2021 Christopher Wolfe (chris@jasuto.com)
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# constant time primality testing, along with a fairly concise prime
# number generator.
# bignum division handles flooring
def floor(n):
return n
def alg_prime_q(ni):
n = ni - 1
return floor((n*2**(n + 1) + 2)//(2*n - (-1)**n + 3)) - floor((n*2**(n + 1) - 2)//(2*n - (-1)**n + 3))
# cos(π x) + i sin(π x)
def phasor(n):
return ((n + 1) & 1) << 1 - 1
# this will include Fermat pseudoprimes as well
def prime_q(n):
q = n - 1
s = q * (1 << (q + 1))
t = (q << 1) - phasor(q) + 3
return floor((s + 2) // t) - floor((s - 2) // t)
# returns all primes < n
def primes(n):
return [i for i in range(3, n, 2) if prime_q(i)]
res = primes(1000)
print(res)
print('done')
【问题讨论】:
-
请在您的问题中解释算法,而不是在链接中。
-
您需要在问题中包含解释,而不是在链接中,正如我所说。而且这个算法绝对不是 O(1) 时间。
-
不,不是。您如何期望计算机在恒定时间内移动任意数量的位?
-
好吧,这真的行不通。我针对低于 200000 的素数对其进行了测试(它变得非常慢,对于较大的 n 值而言,它比线性更慢)。大约需要 20 秒,而 stackoverflow.com/questions/2068372/… 中的函数只需要几分之一秒。你的代码在这个值下给出了 18089 个素数,而实际上只有 17984 - 95 太多了......
-
至于“我还有很多要发布,但我想我会开始轻”,我建议你从“这是正确的?”而不是“这是小说吗?”。如果像在这种情况下,您已经知道它不正确,但您认为它可以“轻松”纠正,那么在请其他人告诉您之前先做所谓的“简单”工作.