Skip to content

Instantly share code, notes, and snippets.

@Gribouillis
Created January 6, 2025 14:37
Show Gist options
  • Save Gribouillis/59e0c9575b79f4872569b7f7b41358b2 to your computer and use it in GitHub Desktop.
Save Gribouillis/59e0c9575b79f4872569b7f7b41358b2 to your computer and use it in GitHub Desktop.
Testing a formula puporting to give the k-th prime number
# testing a formula for the k-th prime number
# https://math.stackexchange.com/questions/5019919/an-exact-formula-for-the-k-th-prime-number
from mpmath import ceil, ln, mp, mpf, zeta
mp.dps = 500
mp.pretty = True
one = mpf("1")
two = mpf("2")
NPRIME = 30
primes = [one for i in range(NPRIME)]
primes[1] = mpf("2")
primes[2] = mpf("3")
def vengy_prime(k):
s = 2 * k * ln(k * ln(k))
prod = one
for j in range(1, k):
prod *= one - primes[j] ** (-s)
# print(repr(prod))
f = (1 - (zeta(s) * prod) ** (-1)) ** (-one / s)
return ceil(f)
for k in range(3, NPRIME):
primes[k] = vengy_prime(k)
print(primes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment