本文整理汇总了C++中BigInteger::is_zero方法的典型用法代码示例。如果您正苦于以下问题:C++ BigInteger::is_zero方法的具体用法?C++ BigInteger::is_zero怎么用?C++ BigInteger::is_zero使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BigInteger
的用法示例。
在下文中一共展示了BigInteger::is_zero方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: next_prime
/**
* 取下一个可能的素数
* 参见java语言BigInteger.nextProbablePrime()实现
*/
NUT_API BigInteger next_prime(const BigInteger& n)
{
if (n <= 1)
return BigInteger(2);
const size_t SMALL_PRIME_THRESHOLD = 95;
const size_t DEFAULT_PRIME_CERTAINTY = 2;
const BigInteger SMALL_PRIME_PRODUCT(((uint64_t) 3) * 5 * 7 * 11 * 13 * 17 * 19 *
23 * 29 * 31 * 37 * 41);
BigInteger result(n);
++result;
// Fastpath for small numbers
if (result.bit_length() < SMALL_PRIME_THRESHOLD)
{
// Ensure an odd number
if (result.bit_at(0) == 0)
++result;
while(true)
{
// Do cheap "pre-test" if applicable
if (result.bit_length() > 6)
{
const int64_t r = (int64_t) (result % SMALL_PRIME_PRODUCT).to_integer();
if ((0 == r % 3) || (0 == r % 5) || (0 == r % 7) || (0 == r % 11) ||
(0 == r % 13) || (0 == r % 17) || (0 == r % 19) || (0 == r % 23) ||
(0 == r % 29) || (0 == r % 31) || (0 == r % 37) || (0 == r % 41))
{
result += 2;
continue; // Candidate is composite; try another
}
}
// All candidates of bit_length 2 and 3 are prime by this point
if (result.bit_length() < 4)
return result;
// The expensive test
if (miller_rabin(result, DEFAULT_PRIME_CERTAINTY))
return result;
result += 2;
}
}
// Start at previous even number
if (result.bit_at(0) == 1)
--result;
// Looking for the next large prime
size_t search_len = (result.bit_length() / 20) * 64;
while (true)
{
BitSieve search_sieve(result, (int) search_len);
const BigInteger candidate = search_sieve.retrieve(result, DEFAULT_PRIME_CERTAINTY);
if (!candidate.is_zero())
return candidate;
result += 2 * search_len;
}
}