Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I improve this Pollard's rho algorithm to handle products of semi-large primes?
    primarykey
    data
    text
    <p>Below is my implementation of Pollard's rho algorithm for prime factorization:</p> <pre class="lang-c++ prettyprint-override"><code>#include &lt;vector&gt; #include &lt;queue&gt; #include &lt;gmpxx.h&gt; // Interface to the GMP random number functions. gmp_randclass rng(gmp_randinit_default); // Returns a divisor of N using Pollard's rho method. mpz_class getDivisor(const mpz_class &amp;N) { mpz_class c = rng.get_z_range(N); mpz_class x = rng.get_z_range(N); mpz_class y = x; mpz_class d = 1; mpz_class z; while (d == 1) { x = (x*x + c) % N; y = (y*y + c) % N; y = (y*y + c) % N; z = x - y; mpz_gcd(d.get_mpz_t(), z.get_mpz_t(), N.get_mpz_t()); } return d; } // Adds the prime factors of N to the given vector. void factor(const mpz_class &amp;N, std::vector&lt;mpz_class&gt; &amp;factors) { std::queue&lt;mpz_class&gt; to_factor; to_factor.push(N); while (!to_factor.empty()) { mpz_class n = to_factor.front(); to_factor.pop(); if (n == 1) { continue; // Trivial factor. } else if (mpz_probab_prime_p(n.get_mpz_t(), 5)) { // n is a prime. factors.push_back(n); } else { // n is a composite, so push its factors on the queue. mpz_class d = getDivisor(n); to_factor.push(d); to_factor.push(n/d); } } } </code></pre> <p>It's essentially a straight translation of the <a href="https://en.wikipedia.org/wiki/Pollard%27s_rho_algorithm" rel="nofollow">pseudocode on Wikipedia</a>, and relies on GMP for big numbers and for primality testing. The implementation works well and can factor primes such as</p> <pre><code>1000036317378699858851366323 = 1000014599 * 1000003357 * 1000018361 </code></pre> <p>but will choke on e.g.</p> <pre><code>1000000000002322140000000048599822299 = 1000000000002301019 * 1000000000000021121 </code></pre> <p>My question is: Is there anything I can do to improve on this, short of switching to a more complex factorization algorithm such as <a href="https://en.wikipedia.org/wiki/Quadratic_sieve" rel="nofollow">Quadratic sieve</a>?</p> <p>I know one improvement could be to first do some trial divisions by pre-computed primes, but that would not help for products of a few large primes such as the above.</p> <p>I'm interested in any tips on improvements to the basic Pollard's rho method to get it to handle larger composites of only a few prime factors. Of course if you find any stupidities in the code above, I'm interested in those as well.</p> <p>For full disclosure: This is a homework assignment, so general tips and pointers are better than fully coded solutions. With this very simple approach I already get a passing grade on the assignment, but would of course like to improve.</p> <p>Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload