快速算法查找最接近较小的质数质数、较小、算法、最接近

2023-09-11 02:43:53 作者:丢了薇笑″嗱什麼来伪装

例如: - 如果给定的数目为10,我们不得不返回7(因为它最接近较小素数)

eg:- If the given number is 10 we have to return 7 (as it the nearest smaller prime number)

我能想到的办法是这样的: -  主循环:给定的数字测试是否是素数还是不(通过应用素性测试),            如果它的首要然后返回数除以1和转到其他主循环递减的数量。

The way I could think of is this:- Mainloop: Test whether the given number is prime or not (by applying a primality test), If its prime then return the number else decrement the number by 1 and goto Mainloop.

不过,我必须努力得到long long int范围,其采取了很多时间。

But I have to work on long long int range and its taking a lot of time.

有没有更好的方法呢,另外,如果我要与上面的方法只能走我应该使用的素性测试呢?谢谢:)

Is there a better approach to it, also if I should go with the above way only then which primality test should I use? thanks :)

推荐答案

如果你输入的大小为界,查找在$ P $的表pcomputed素数将​​可能是最快的。

If the size of your inputs is bounded, lookup in a table of precomputed primes will probably be the fastest.