Better way to find all the prime factors of huge integers in C?

前提是你 提交于 2020-07-10 08:25:10

问题


I have written a code in C that basically makes a list of all the prime factors of a huge number, which is stored using the gmp library. Here it is :

int is_div(mpz_t number, mpz_t i) {
    return mpz_divisible_p(number,i)!=0;
}

mpz_t * prime_divs(mpz_t number){
    mpz_t * prime_dividers = NULL;
    mpz_t i, i_squared,TWO, comp;
    mpz_inits(i, i_squared, TWO, comp, NULL);
    mpz_set_ui(i,2);
    mpz_mul(i_squared, i ,TWO);
    while(mpz_cmp(i_squared,number)<=0){
        if(is_div(number,i)){
            mpz_fdiv_q(comp, number, i);
            if(is_prime(i)) append(&prime_dividers,i);
            if(is_prime(comp)) append(&prime_dividers,comp);
        }
        mpz_add_ui(i,i,1);
        mpz_mul(i_squared, i ,i);
    }
    mpz_clears(i, i_squared, TWO, comp, NULL);
    return prime_dividers;
}

Note that the function int is_prime(mpz_t n) is not defined here because it is quite long. Just know that it is an implementation of a deterministic variant (up to 3,317,044,064,679,887,385,961,981) of Miller-Rabin's primality test. Same goes for the function void append(mpz_t** arr, mpz_t i), it is just a function that appends it to a list.

So my prime_divs function searches for all integers iin the range [2,sqrt(number)] which divide number. If it is the case, it then calculates it's complementary divisor (i.e. number/i) and determines if any of them are primes. Would these integers be prime, then they would be appended to a list using append.

Is there any way to makeprime_divs faster?


回答1:


I suspect you can save time by first checking for small divisors. Use the Sieve of Eratosthenes to set up a list of prime numbers below 5,000 or 10,000. Then use that list to find the small factors, if any, of your large number. Every time you find a factor (possibly multiple times for the same factor) divide out that factor to reduce the target number's size.

When you have exhausted the list of small primes, it may be worth running a quick primality check on the large residue before trying to factor it. This avoids wasting a lot of time looking for factors of a large prime. You will need to test this idea to see if it actually saves time for you.

Only then should you call the M-R test to find the remaining factors.



来源:https://stackoverflow.com/questions/61313968/better-way-to-find-all-the-prime-factors-of-huge-integers-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!