Every odd integer $n$ is represented by the ternary quadratic form $n = 2x^2 + y^2 + z^2$.
By Fermat's Sum of Squares Theorem, primes of the form $p = 4k+1$ can be represented as $p = 4k+1 = a^2 +b^2$.
Suppose $n = (2a_1^2 + a_2^2 + a_3^2)(2b_1^2 + b_2^2 + b_3^2)$. WLOG take $2a_1^2 + a_2^2 + a_3^2 \le 2b_1^2 + b_2^2 + b_3^2$. The equality holds when $n$ is a square. Assume $n$ is not a square. Therefore, we have the inequality,
$$ 2a_1^2 + a_2^2 + a_3^2 < \sqrt{n} < 2b_1^2 + b_2^2 + b_3^2 < n $$
By taking $a_2^2 + a_3^2 = p = 4k+1$, we obtain the following algorithm for factoring $n$.
Algorithm Factor($n$)
Input: $n$: An odd positive integer that is not a square
Returns: $\begin{cases} 1 : \text{if $n$ is prime}, \\ a : \text{where $a|n$, if $n$ is composite} \end{cases} $
Begin
Let $\beta = \bigg\lfloor \sqrt{\frac{\sqrt{n}}{2}} \bigg\rfloor$
For $a_1 = 0,1, \cdots \beta$
a. Let $\delta = 2a_1^2$
b. Let $k_{\text{max}} = \frac{\sqrt{n} - \delta - 1}{4}$
c. For $k = 1, 2, \cdots, k_{\text{max}}$
Let $p = 4k + 1$
Let $a = \delta + p$
If $p$ is prime by Miller-Rabin test then
a. If $a|n$ then
- return $a$ $\text{ ; Found $a$ as a factor of $n$}$
c. End If
End If
d. Next
Next $a_1$
return $1$ $\text{ ; $n$ is prime}$
End
In step $2.c.3$, we do a Miller-Rabin primality test on $p$. This is not required and we can simply check $a|n$ even if $p$ is not prime. The algorithmic complexity remains unchanged.
Termination. The algorithm runs a finite number of times. It terminates in step $2.c.3.a.1$ or in step $4.$
Correctness. The algorithm is correct based on the following claims:
- The outer loop in step $2$ ensures that we check each $\delta = 2a_1^2 \lt \sqrt{n}$.
- The inner loop in step $2.c$ checks for all integers $p$ of the form $4k+1$ satisfying $\delta + p \lt \sqrt{n}$.
- (Claim A): Since this represents all odd integers, the subset of primes $\lt \sqrt{n}$ are covered by this search.
- Therefore, the algorithm finds the odd divisor of $n$ (which is also odd).
- (Claim B): If we do not find an odd integer of the form $2a_1^2 + a_2^2 + a_3^2$ then $n$ has no divisors less than $\sqrt{n}$ which implies $n$ must be prime.
Time Complexity.
- Excluding the Miller-Rabin test: If the Miller-Rabin test is not included, then the time complexity is:
$$ \begin{align} \overbrace{O\bigg(\frac{1}{\sqrt{2}}n^\frac{1}{4}\bigg)}^{\text{outer loop}} \times \overbrace{O\bigg(\frac{n^{\frac{1}{2}} - 1}{4}\bigg)}^{\text{inner loop}} \approx O\bigg(\frac{n^\frac{1}{4}}{\sqrt{2}} \cdot \big(\frac{n^{\frac{1}{2}} - 1}{4}\big) \bigg) \approx O\bigg(\frac{n^\frac{1}{4}}{\sqrt{2}} \cdot \frac{n^{\frac{1}{2}}}{4} \bigg) \approx O\bigg(\frac{n^\frac{3}{4}}{4\sqrt{2}}\bigg) \end{align} $$
- Including the Miller-Rabin test: If the Miller-Rabin test is included, then the time complexity includes a factor $O(K \cdot (\log^3 n))$ i.e.,
$$O\bigg(K \cdot (\log^3 n) \cdot \frac{n^\frac{3}{4}}{4\sqrt{2}}\bigg)$$
where $K$ is the number of iterations in the Miller-Rabin test.
This is not better than trial division which is $O(\sqrt{n})$.
Question: Can we prove claims $A$ and $B$?