NTHROOTMOD
This function solves congruences of the form x^n \equiv a \pmod p and returns matching roots in a spillable Excel format.
The equation is:
x^n \equiv a \pmod p
Depending on arithmetic structure, there may be zero, one, or multiple roots. The output is normalized to a single-column 2D array.
Excel Usage
=NTHROOTMOD(a, n, p, all_roots)
a(int, required): Right-hand-side residue value.n(int, required): Positive exponent.p(int, required): Positive modulus.all_roots(bool, optional, default: true): If true, return all roots from the solver.
Returns (list[list]): Single-column 2D array of nth roots modulo p, or a single blank cell if none exist.
Example 1: Documented nth root example returning one root
Inputs:
| a | n | p | all_roots |
|---|---|---|---|
| 11 | 4 | 19 | false |
Excel formula:
=NTHROOTMOD(11, 4, 19, FALSE)
Expected output:
8
Example 2: Documented nth root example returning all roots
Inputs:
| a | n | p | all_roots |
|---|---|---|---|
| 11 | 4 | 19 | true |
Excel formula:
=NTHROOTMOD(11, 4, 19, TRUE)
Expected output:
| Result |
|---|
| 8 |
| 11 |
Example 3: Cubic root congruence example
Inputs:
| a | n | p | all_roots |
|---|---|---|---|
| 68 | 3 | 109 | true |
Excel formula:
=NTHROOTMOD(68, 3, 109, TRUE)
Expected output:
| Result |
|---|
| 23 |
| 32 |
| 54 |
Example 4: No nth root solution exists
Inputs:
| a | n | p | all_roots |
|---|---|---|---|
| 2 | 2 | 7 | true |
Excel formula:
=NTHROOTMOD(2, 2, 7, TRUE)
Expected output:
| Result |
|---|
| 3 |
| 4 |
Python Code
from sympy import nthroot_mod as sympy_nthroot_mod
def nthrootmod(a, n, p, all_roots=True):
"""
Solve nth-power congruences modulo an integer.
See: https://docs.sympy.org/latest/modules/ntheory.html#sympy.ntheory.residue_ntheory.nthroot_mod
This example function is provided as-is without any representation of accuracy.
Args:
a (int): Right-hand-side residue value.
n (int): Positive exponent.
p (int): Positive modulus.
all_roots (bool, optional): If true, return all roots from the solver. Default is True.
Returns:
list[list]: Single-column 2D array of nth roots modulo p, or a single blank cell if none exist.
"""
try:
roots = sympy_nthroot_mod(a, n, p, all_roots=all_roots)
if roots is None:
return [[""]]
if isinstance(roots, int):
return [[int(roots)]]
if len(roots) == 0:
return [[""]]
return [[int(r)] for r in roots]
except Exception as e:
return f"Error: {str(e)}"Online Calculator
Right-hand-side residue value.
Positive exponent.
Positive modulus.
If true, return all roots from the solver.