Complex plot of the Analytic version
I've been doing some number theory during all this Covid downtime and I think I've discovered an pretty interesting (if not novel) algorithm for detecting primality. I posted my writeup on my LinkedIn page, you don't have to signup or anything.
https://www.linkedin.com/pulse/efficient-prime-number-generation-christopher-wolfe/
I just want to know if this technique is already known, because it's quite fast (constant time) and pretty compact in size. I did a deeper dive a while back which you can read on my blog.
http://jasuto.com/ideas/primes/
It would be great to get some feedback and to verify that this is new. I have quite a bit more to release, but thought I would start light :)
Here is the demonstration code if you don't feel like reading the article. I have seen many prime number implementations, but nothing O(1) or this small...
# 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')