Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(math.py): Add the functionality to generate a random prime number within a specified range #164

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

weilycoder
Copy link
Contributor

@weilycoder weilycoder commented Feb 16, 2025

Update2: 另外,相关函数放在 math.py 中是否合理?


算法不保证均匀随机,如果追求区间内每个质数被选中的概率相等,可以使用 prime_sieve 预处理质数表再使用 random 库中的 choice 函数。

具体流程如下:

  1. 在区间内随机一个数字;
  2. 判断这个数是否是质数,若是则立即返回;
  3. 寻找这个数字的下一个质数,若在范围内则立即返回;
  4. 寻找这个数字的上一个质数,若在范围内则立即返回;
  5. 范围内没有质数,报告错误。

Update: 已经对函数进行测试,与暴力查找进行了对拍,似乎没有问题


我为此实现的 nextprimeprevprime 都未经严格测试,大家可以先 review 一下(使用了大于 $3$ 的质数都是 $6n\pm 1$ 型的定理,逻辑略复杂)。

测试还没写,一会补上。

@weilycoder weilycoder changed the title 增加在指定范围内随机质数的功能 feat(math.py): 增加在指定范围内随机质数的功能 Feb 16, 2025
@weilycoder weilycoder mentioned this pull request Feb 16, 2025
@weilycoder weilycoder changed the title feat(math.py): 增加在指定范围内随机质数的功能 feat(math.py): Add the functionality to generate a random prime number within a specified range Feb 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant