Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

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

Open
weilycoder wants to merge 4 commits into luogu-dev:master
base: master
Choose a base branch
Loading
from weilycoder:dev-randprime

Conversation

@weilycoder
Copy link
Contributor

@weilycoder weilycoder commented Feb 16, 2025
edited
Loading

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


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

具体流程如下:

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

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


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

(削除) 测试还没写,一会补上。 (削除ここまで)

@weilycoder weilycoder changed the title (削除) 增加在指定范围内随机质数的功能 (削除ここまで) (追記) feat(math.py): 增加在指定范围内随机质数的功能 (追記ここまで) 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

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

1 participant

AltStyle によって変換されたページ (->オリジナル) /