Find what 2 numbers add to something and multiply to something

Find what 2 numbers add to something and multiply to something

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Solving Quadratic Systems Without Brute Force

When you need to find two numbers that multiply to a specific value and add to another, you are essentially solving for the roots of a quadratic equation. In the context of factoring programs or general numerical analysis, this problem often arises when trying to decompose expressions like $x^2 - (a+b)x + ab = 0$ into $(x-a)(x-b)$. The challenge lies in doing this efficiently, especially when dealing with large integers or floating-point numbers where brute-force iteration becomes computationally expensive.

Consider the system of equations provided:
$$ab = P$$
$$a + b = S$$

Where $P$ is the product and $S$ is the sum. The naive approach involves iterating through every integer factor of $P$ to see if any pair adds up to $S$. While this works for small numbers, it scales poorly with time complexity relative to the magnitude of the input. A more robust solution leverages algebraic substitution to transform the problem into a standard quadratic formula calculation.

You can derive the individual values by treating one variable as a function of the other. Since $b = P/a$, you can substitute this into the sum equation:
$$a + \frac{P}{a} = S$$

Multiplying the entire equation by $a$ clears the denominator:
$$a^2 + P = Sa$$
$$a^2 - Sa + P = 0$$

This is now a standard quadratic equation in the form $ax^2 + bx + c = 0$, where the variable is $a$. The coefficients are derived directly from your inputs: the coefficient of the squared term is $1$, the linear term is $-S$, and the constant term is $P$.

To solve for $a$ (and consequently $b$), apply the quadratic formula:
$$a = \frac{S \pm \sqrt{S^2 - 4P}}{2}$$

This approach has a significant advantage over iteration because it runs in constant time, $O(1)$, assuming basic arithmetic operations take constant time. This is critical for high-performance applications or when processing streams of data where latency matters.

However, there are important implementation details to consider. The term under the square root, the discriminant ($\Delta = S^2 - 4P$), determines the nature of the roots. If $\Delta > 0$, you have two distinct real solutions. If $\Delta = 0$, the two numbers are identical. If $\Delta < 0$, there are no real solutions, meaning the input parameters $S$ and $P$ cannot form a valid pair of real numbers.

Handling Negative Numbers and Real-World Constraints

The original query explicitly mentions that negative numbers must be considered. The quadratic formula handles this automatically; if $P$ is negative, the discriminant will naturally reflect the expanded magnitude required for the roots to exist. For instance, if $ab = -6$ and $a+b=1$, the equation becomes $x^2 - x - 6 = 0$. Solving this yields roots of $3$ and $-2$, which correctly satisfy both the product ($3 \times -2 = -6$) and sum ($3 + -2 = 1$) constraints.

When implementing this in code, floating-point precision becomes a potential pitfall. Using standard floating-point types like float or double can introduce rounding errors, particularly when $S^2$ is very large compared to $4P$. If you are building a factoring program for cryptographic applications or high-precision arithmetic, relying on the quadratic formula with floats might yield incorrect integer results due to epsilon errors.

In such cases, it is often better to use an integer-based approach if you know the inputs are integers. You can calculate the discriminant using big integer libraries (available in languages like Python, Java, or via Composer packages in PHP) before taking the square root. Only after verifying that the discriminant is a perfect square should you extract the root to ensure the resulting numbers are exact integers.

If you are working within the Laravel ecosystem and dealing with heavy mathematical computations, you might consider offloading these calculations to a service designed for complex logic or using packages like bcmath for arbitrary-precision arithmetic. While Laravel provides many utilities out of the box, it does not include built-in arbitrary-precision math functions by default; relying on standard PHP types will lead to precision loss with very large inputs. For most web applications, however, standard double-precision floats are sufficient unless you are specifically targeting edge cases in numerical stability.

Ultimately, transforming the problem into a quadratic equation provides the most efficient path forward. It avoids unnecessary loops and handles negative values natively through the algebraic structure of the solution.

Our network