恩,使用GNU的高精度库写了一个,答案一样……
要编译的话,必须包含libgmp,在Ubuntu下可以:
sudo apt-get install libgmp3-dev
来获得gmp库,编译的时候加上-lgmp参数。
要编译的话,必须包含libgmp,在Ubuntu下可以:
sudo apt-get install libgmp3-dev
来获得gmp库,编译的时候加上-lgmp参数。
程序代码:
#include <stdio.h> #include <gmp.h> #include <stdlib.h> mpz_t m; void dfs(mpz_t n, mpz_t deep) { int i; mpz_t new_n, new_deep; mpz_init(new_n); mpz_init(new_deep); mpz_mul_ui(new_deep, deep, 10); if (mpz_cmp(m, n) < 0) mpz_set(m, n); for (i = 1; i < 10; ++i) { mpz_set(new_n, n); mpz_addmul_ui(new_n, new_deep, i); if (mpz_probab_prime_p(new_n, 20)) dfs(new_n, new_deep); } mpz_clear(new_n); mpz_clear(new_deep); } int main(void) { mpz_t n, deep; mpz_init(m); mpz_init(n); mpz_init(deep); mpz_set_ui(deep, 1); mpz_set_ui(n, 3); dfs(n, deep); mpz_set_ui(n, 5); dfs(n, deep); mpz_set_ui(n, 7); dfs(n, deep); mpz_out_str(stdout, 10, m); mpz_clear(deep); mpz_clear(n); mpz_clear(m); return 0; } /* cc: flags+='-lgmp' */