简体   繁体   English

在 Python 中采样大于 p 的 n 个随机素数的最快方法?

[英]Fastest way to sample n random primes greater than p in Python?

I want to create a dateset of n primes greater than p ~ 2⁵⁰.我想创建一个大于 p ~ 2⁵⁰ 的 n 个素数的日期集。 I want these primes to not be consecutive but have some space in between so the difference between iᵗʰ and (i+1)ᵗʰ prime is not just a few bits.我希望这些素数不是连续的,但中间有一些空间,所以 iᵗʰ 和 (i+1)ᵗʰ 素数之间的区别不仅仅是几位。

I am using Sympy's randprime(low, hi) in a loop,我在循环中使用 Sympy 的randprime(low, hi)

p = [start]
for i in range(n):
    curr = int(randprime(start, 2 * start + 1))
    p.append(curr)
    start = curr

This gets significantly slow for n=10,000 .对于n=10,000这会变得非常慢。 Is there a better (faster) way of accomplishing the prime sampling that I want?有没有更好(更快)的方法来完成我想要的素数采样?

Precompute (or just download ) a list of primes once and then sample the list during the runtime.预先计算(或只是下载)一个素数列表,然后在运行时对列表进行采样。

You can generate the list for 30x smaller upper bound than 2^50 (~10^15) with this algorithm: https://primes.utm.edu/nthprime/algorithm.php您可以使用此算法生成比 2^50(~10^15)小 30 倍的上限列表: https : //primes.utm.edu/nthprime/algorithm.php

I don't know how to get further with reasonable hardware setup.我不知道如何通过合理的硬件设置走得更远。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 列出 N 以下所有素数的最快方法 - Fastest way to list all primes below N 从带有排除项的 Python 列表中随机抽取 N 个元素的最快方法 - Fastest way to randomly sample N elements from a Python list with exclusions 大于当前键值时在python中设置字典值的最快方法 - Fastest way to set a dictionary value in python when greater than current key value 使用 Python 在数组中用 0 替换负值和用 1 替换大于 1 的值的最快方法是什么? - What is the fastest way to replace negative values with 0 and values greater than 1 with 1 in an array using Python? 以最小差异大于 Python 列表中的值对大多数数字进行采样的最快方法 - Fastest way to sample most numbers with minimum difference larger than a value from a Python list 对 numpy 数组的许多随机排列进行采样的最快方法 - fastest way to sample many random permutations of a numpy array 给定一个数字 p,打印 n 的值,其中 (1/1 + 1/2 +... + 1/n) 的总和大于 p - given a number p, print value of n at which sum of (1/1 + 1/2 + ... + 1/n) is greater than p Python:创建一个包含 n 个列表的列表的最快方法 - Python: fastest way to create a list of n lists 在 python 中舍入随机数的最快方法 - Fastest way to round random numbers in python random.sample python 中的“样本大于总体” - “sample larger than population” in random.sample python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM