Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Suppose I know the value of $n$ (where $n = pq$), and I also know $k = p-q$. How can I efficiently factor $n$?

Note that I don't know $p$ or $q$.

EDIT: Thank you for your answers. I understand that I could use basic algebra but would it be efficient to use that if $n$ was large? For example, if $n = 59305397$?

share|cite|improve this question
$\rm n=pq=(q+k)q$ yields $\rm q^2+kq-n=0$, which is a quadratic equation. – anon Mar 5 '13 at 4:33
Are you computing by hand? Or by computer? Do you really mean numbers as small as 8 digits? – Hurkyl Mar 5 '13 at 6:04
I am am computing using a standard scientific calculator, not a computer (or writing a program or anything of that sort). – chromozonex Mar 5 '13 at 6:11
up vote 5 down vote accepted

You have a system of two equations in two unknowns; you can appeal to high school algebra!

share|cite|improve this answer

We assume that $p$ and $q$ are huge.

Note that $(p+q)^2=(p-q)^2+4pq$. We know $p-q$ and $pq$, so can compute $(p+q)^2$ cheaply. Now we can compute $p+q$ cheaply, by an adaptation of Newton's Method.

We now know $p-q$ and $p+q$. Calculate $\frac{1}{2}\left((p-q)+(p+q)\right)$.

share|cite|improve this answer

Hint $\ $ Since we have been given both the $\rm\color{#C00}{sum}$ and $\rm\color{#0A0}{product}$ of $\rm\,p\,$ and $\rm\,-q,\,$ we know the coefficients of the quadratic polynomial having $\rm\,p\,$ and $\rm\,-q\,$ as roots, namely

$$\rm (x-p)(x+q)\, =\, x^2 - (\color{#C00}{p\!-\!q})\, x \color{#0A0}{-pq}\ =\ x^2 - k\, x -n $$

Therefore, solving $\rm\ x^2 - k\, x -n\, =\, 0\ $ yields the roots $\rm\:p\:$ and $\rm\:-q.$

share|cite|improve this answer

Note that $p=k+q$, so $n=q(k+q)$ or $q^2+kq-n=0$. This is a quadratic in $q$.

share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.