十亿分之一素数算法和语言 [已关闭]

Billionth prime number algorithm and language [closed]

提问人:Jman5213 提问时间:11/13/2023 更新时间:11/13/2023 访问量:96

问:


想改进这个问题吗?通过编辑这篇文章添加详细信息并澄清问题。

7天前关闭。

我想在没有非常大的计算机的情况下快速计算至少十亿分之一的素数。我应该使用什么方法和编程语言来避免内存不足,并且仍然在合理的时间内完成程序?

我用这个脚本尝试过python:

from math import sqrt
from time import perf_counter
c=int(input("calculate the nth prime #: "))
n=1#currentNum
f=0#primesFound
l=1#latestPrime
x=perf_counter()
def p(f):
 for i in range(3,int(sqrt(f)+1),2):
  if f%i==0:
    return False
 return True
while f<c:
 if p(n):
  f+=1
  l=n
 n+=2
y=perf_counter()
print('Your num:',str(l)+'\nTime:',str(y-x)+"sec")

我也尝试过在 python 中使用 eratosthenes 的 seive,但以下代码遇到了内存错误:

import math
from time import perf_counter

def sieve_of_eratosthenes(limit):
    sieve = [True] * (limit + 1)
    sieve[0] = sieve[1] = False

    for p in range(2, int(limit ** 0.5) + 1):
        if sieve[p]:
            for i in range(p * p, limit + 1, p):
                sieve[i] = False

    primes = [p for p, is_prime in enumerate(sieve) if is_prime]
    return primes

c = int(input("Calculate the nth prime num: "))
x = perf_counter()

"""Calculate the estimated upper limit based on the nth prime"""
estimated_limit = int(c * (math.log(c) + math.log(math.log(c))))
primes = sieve_of_eratosthenes(estimated_limit)

y = perf_counter()

if c <= len(primes):
    nth_prime = primes[c - 1]
    print('Your num:', nth_prime, '\nTime:', y - x, "sec")
else:
    print('There are not enough prime numbers in the precomputed list.')

javascript python java c++ 算法

评论

0赞 Some programmer dude 11/13/2023
欢迎使用 Stack Overflow。请阅读帮助页面,参加 SO 导览,并阅读如何提问。另外,请阅读如何写出“完美”的问题,尤其是它的清单
1赞 Klaus D. 11/13/2023
互联网上的大多数加密都是基于这样一个事实,即这个问题极难解决。因此,没有已知的低工作量解决方案。
3赞 Selcuk 11/13/2023
@KlausD。质因式分解很难,但找到第 n 个素数并不。事实上,十亿分之一素数并不是一个很大的数字。它是 22,801,763,489。
4赞 Alan Birtles 11/13/2023
@molbdnilo不需要存储埃拉托色尼筛子的数字,你只需要每个数字的一个布尔值,通过有效的打包,你需要24千兆位或3千兆字节的内存。如果你忽略偶数,你可以把它减半
4赞 Botje 11/13/2023
@Spektre热循环中运行的 Java 代码将被编译为本机代码,因此将其称为“解释”是不公平的。

答: 暂无答案