QUADCONG

This function solves quadratic congruences of the form a x^2 + b x + c \equiv 0 \pmod n.

The target equation is:

a x^2 + b x + c \equiv 0 \pmod n

Solutions are returned as a sorted list by SymPy and converted into a single-column 2D array for Excel output.

Excel Usage

=QUADCONG(a, b, c, n)
  • a (int, required): Quadratic coefficient.
  • b (int, required): Linear coefficient.
  • c (int, required): Constant coefficient.
  • n (int, required): Positive modulus.

Returns (list[list]): Single-column 2D array of solutions modulo n, or a single blank cell if none exist.

Example 1: Quadratic congruence with two solutions

Inputs:

a b c n
2 5 3 7

Excel formula:

=QUADCONG(2, 5, 3, 7)

Expected output:

Result
2
6
Example 2: Quadratic congruence with no solution returns blank cell

Inputs:

a b c n
8 6 4 15

Excel formula:

=QUADCONG(8, 6, 4, 15)

Expected output:

""

Example 3: x squared congruence transformed form

Inputs:

a b c n
1 0 -1 8

Excel formula:

=QUADCONG(1, 0, -1, 8)

Expected output:

Result
1
3
5
7
Example 4: Shifted monic quadratic congruence

Inputs:

a b c n
1 -3 2 11

Excel formula:

=QUADCONG(1, -3, 2, 11)

Expected output:

Result
1
2

Python Code

from sympy import quadratic_congruence as sympy_quadratic_congruence

def quadcong(a, b, c, n):
    """
    Solve quadratic congruences modulo n.

    See: https://docs.sympy.org/latest/modules/ntheory.html#sympy.ntheory.residue_ntheory.quadratic_congruence

    This example function is provided as-is without any representation of accuracy.

    Args:
        a (int): Quadratic coefficient.
        b (int): Linear coefficient.
        c (int): Constant coefficient.
        n (int): Positive modulus.

    Returns:
        list[list]: Single-column 2D array of solutions modulo n, or a single blank cell if none exist.
    """
    try:
        sols = sympy_quadratic_congruence(a, b, c, n)
        if not sols:
            return [[""]]
        return [[int(v)] for v in sols]
    except Exception as e:
        return f"Error: {str(e)}"

Online Calculator

Quadratic coefficient.
Linear coefficient.
Constant coefficient.
Positive modulus.