因此,我使用C語言的GMP庫來查找高於某個值的Twin素數。雖然我確信自己的策略能夠奏效,但問題變成了這樣一個事實,那就是花費大量時間(我知道在找到更高級別的素材時遇到困難。)有沒有一種方法可以優化搜索?下面是我的代碼的一個片段:如何提高查找雙子彈
mpz_ui_pow_ui(a, base, exponent);
mpz_nextprime(b, a); // b is the next prime number after a.
// c and d will be prime + 2 and
// prime - 2.
/* Fortunate of fortunalities, mpz_nextprime gives the next
prime greater than what one adds in! */
/* We need to test if numbers are prime too. */
while (al == false) {
mpz_add_ui (c, b, 2);
mpz_add_ui (d, b, -2);
if ((mpz_probab_prime_p(c, 15) == 2) ||
(mpz_probab_prime_p(d, 15) == 2)) { // Returns 2
// if c/d are
// definitely
// prime.
mpz_set(firstprime,b);
al == true;
break;
}
{
mpz_nextprime(b, b); // b is the next prime number
// after a. c and d will be
// prime + 2 and prime - 2.
}
}
printf("first twin is: ");
mpz_out_str(stdout, 10, firstprime);
printf("\n");
printf("second twin is: ");
if (mpz_probab_prime_p(c, 15) == 2) {
mpz_out_str(stdout, 10, c);
} else {
mpz_out_str(stdout, 10, d);
}
printf ("\n");
還有其他一些檢查大質數的方法,比如Miller-Rabin素性檢驗,而沒有詳盡地找到所有較低質數。 –
當'mp'_probab_prime_p'爲'c'和'd'返回1時,您會忽略這種情況。 – jxh
mpz probab Miller-Rabin。 – Marorin