Modular Arithmetic
Overview
Modular arithmetic studies integer values up to congruence classes, where numbers are equivalent if they differ by a multiple of a modulus. It is the language behind periodic systems, residue classes, and many cryptographic constructions, and is central to modular arithmetic in number theory. In data and engineering contexts, modular methods support cycle alignment, checksum logic, finite-state behavior, and secure key operations. This category focuses on solving congruences, testing residue properties, and reasoning about multiplicative structure modulo n.
At a conceptual level, these tools connect group order, residuosity, and congruence solving. Invertible residues modulo n form a multiplicative group, and functions here ask whether an element generates that group, whether an element is an r-th power residue, and how to recover unknown exponents or roots. The core relationships include a^k \equiv 1 \pmod n for multiplicative order, x^2 \equiv a \pmod p for quadratic residuosity, and the generalized root equation x^m \equiv a \pmod n. Together, these ideas make modular systems computable and testable.
Implementations are powered by SymPy number-theory routines, primarily the residue number theory module. SymPy provides exact integer algorithms for discrete logarithms, primitive roots, symbol computations, and modular root finding. This gives spreadsheet-style workflows access to mathematically rigorous methods without requiring custom low-level implementations.
DLOG, NORDER, ISPRIMROOT, and PRIMROOT cover multiplicative-group structure and generators. NORDER computes the multiplicative order of an element, while ISPRIMROOT and PRIMROOT test or construct generators of the full unit group when primitive roots exist. DLOG solves b^x \equiv a \pmod n, the inverse of modular exponentiation and a key hard problem in public-key cryptography. In practice, this group supports generator selection, subgroup analysis, and security intuition for cyclic-group protocols.
ISQUADRES, LEGENDRE, JACOBI, and QUADRES characterize quadratic residuosity from complementary angles. ISQUADRES directly checks whether a quadratic congruence has a solution, while QUADRES enumerates quadratic residues modulo a given modulus. LEGENDRE provides a prime-modulus residue indicator, and JACOBI extends symbol-based testing to odd composite moduli, where interpretation requires more care. These tools are useful for screening solvability quickly, building residue-class diagnostics, and teaching reciprocity-driven reasoning.
SQRTMOD, NTHROOTMOD, and QUADCONG solve explicit modular equations. SQRTMOD finds solutions of x^2 \equiv a \pmod n, while NTHROOTMOD generalizes to x^m \equiv a \pmod n for higher-power roots. QUADCONG solves full quadratic congruences of the form ax^2+bx+c \equiv 0 \pmod n, bridging symbolic polynomial forms and residue-class solution sets. These functions are practical in coding theory, modular constraint systems, and algorithmic number-theory pipelines where existence and enumeration of roots determine downstream behavior.
DLOG
This function computes the discrete logarithm x such that b^x \equiv a \pmod n, when a solution exists.
The problem is:
b^x \equiv a \pmod n
Discrete logarithms are fundamental in computational number theory and cryptography. The implementation uses SymPy’s internal strategy selection across algorithms such as baby-step giant-step and Pohlig-Hellman.
Excel Usage
=DLOG(n, a, b, order, prime_order)
n(int, required): Modulus of the congruence.a(int, required): Target residue value.b(int, required): Base of exponentiation.order(int, optional, default: null): Optional order of the subgroup containing the base.prime_order(bool, optional, default: null): Whether the subgroup order is known to be prime.
Returns (int): Exponent x satisfying b**x ≡ a (mod n).
Example 1: Discrete logarithm documented example
Inputs:
| n | a | b | order | prime_order |
|---|---|---|---|---|
| 41 | 15 | 7 |
Excel formula:
=DLOG(41, 15, 7, , )
Expected output:
3
Example 2: Discrete logarithm modulo a small prime
Inputs:
| n | a | b | order | prime_order |
|---|---|---|---|---|
| 17 | 13 | 3 |
Excel formula:
=DLOG(17, 13, 3, , )
Expected output:
4
Example 3: Discrete logarithm with power of two exponent
Inputs:
| n | a | b | order | prime_order |
|---|---|---|---|---|
| 29 | 16 | 2 |
Excel formula:
=DLOG(29, 16, 2, , )
Expected output:
4
Example 4: Discrete logarithm using provided subgroup order
Inputs:
| n | a | b | order | prime_order |
|---|---|---|---|---|
| 17 | 4 | 2 | 8 |
Excel formula:
=DLOG(17, 4, 2, 8, )
Expected output:
2
Python Code
Show Code
from sympy import discrete_log as sympy_discrete_log
def dlog(n, a, b, order=None, prime_order=None):
"""
Solve discrete logarithms in modular arithmetic.
See: https://docs.sympy.org/latest/modules/ntheory.html#sympy.ntheory.residue_ntheory.discrete_log
This example function is provided as-is without any representation of accuracy.
Args:
n (int): Modulus of the congruence.
a (int): Target residue value.
b (int): Base of exponentiation.
order (int, optional): Optional order of the subgroup containing the base. Default is None.
prime_order (bool, optional): Whether the subgroup order is known to be prime. Default is None.
Returns:
int: Exponent x satisfying b**x ≡ a (mod n).
"""
try:
return int(sympy_discrete_log(n, a, b, order=order, prime_order=prime_order))
except Exception as e:
return f"Error: {str(e)}"Online Calculator
ISPRIMROOT
This function tests whether a is a primitive root modulo p. A value is a primitive root when it generates the entire multiplicative group of units modulo p.
Equivalently, a must satisfy:
\gcd(a,p)=1 \quad \text{and} \quad \operatorname{ord}_p(a)=\varphi(p)
Primitive roots exist only for moduli in specific families, so many moduli return false for all candidates.
Excel Usage
=ISPRIMROOT(a, p)
a(int, required): Candidate generator value.p(int, required): Modulus greater than one.
Returns (bool): True if a is a primitive root of p, otherwise False.
Example 1: Primitive root true case modulo ten
Inputs:
| a | p |
|---|---|
| 3 | 10 |
Excel formula:
=ISPRIMROOT(3, 10)
Expected output:
true
Example 2: Primitive root false case modulo ten
Inputs:
| a | p |
|---|---|
| 9 | 10 |
Excel formula:
=ISPRIMROOT(9, 10)
Expected output:
false
Example 3: Primitive root true case modulo prime
Inputs:
| a | p |
|---|---|
| 2 | 11 |
Excel formula:
=ISPRIMROOT(2, 11)
Expected output:
true
Example 4: Primitive root false case modulo prime
Inputs:
| a | p |
|---|---|
| 4 | 11 |
Excel formula:
=ISPRIMROOT(4, 11)
Expected output:
false
Python Code
Show Code
from sympy import is_primitive_root as sympy_is_primitive_root
def isprimroot(a, p):
"""
Check whether a value is a primitive root modulo n.
See: https://docs.sympy.org/latest/modules/ntheory.html#sympy.ntheory.residue_ntheory.is_primitive_root
This example function is provided as-is without any representation of accuracy.
Args:
a (int): Candidate generator value.
p (int): Modulus greater than one.
Returns:
bool: True if a is a primitive root of p, otherwise False.
"""
try:
return bool(sympy_is_primitive_root(a, p))
except Exception as e:
return f"Error: {str(e)}"Online Calculator
ISQUADRES
This function determines whether the congruence x^2 \equiv a \pmod p has at least one solution.
In set form, it checks whether a \bmod p belongs to:
\{x^2 \bmod p : x = 0,1,\dots,p-1\}
The result is a boolean indicator often used before attempting modular square root computations.
Excel Usage
=ISQUADRES(a, p)
a(int, required): Value to test for quadratic residuosity.p(int, required): Positive modulus.
Returns (bool): True if a is a quadratic residue modulo p, otherwise False.
Example 1: Quadratic residue true case with composite modulus
Inputs:
| a | p |
|---|---|
| 21 | 100 |
Excel formula:
=ISQUADRES(21, 100)
Expected output:
true
Example 2: Quadratic residue false case with composite modulus
Inputs:
| a | p |
|---|---|
| 21 | 120 |
Excel formula:
=ISQUADRES(21, 120)
Expected output:
false
Example 3: Quadratic residue true case modulo prime
Inputs:
| a | p |
|---|---|
| 2 | 7 |
Excel formula:
=ISQUADRES(2, 7)
Expected output:
true
Example 4: Quadratic residue false case modulo prime
Inputs:
| a | p |
|---|---|
| 3 | 7 |
Excel formula:
=ISQUADRES(3, 7)
Expected output:
false
Python Code
Show Code
from sympy import is_quad_residue as sympy_is_quad_residue
def isquadres(a, p):
"""
Check whether a value is a quadratic residue modulo p.
See: https://docs.sympy.org/latest/modules/ntheory.html#sympy.ntheory.residue_ntheory.is_quad_residue
This example function is provided as-is without any representation of accuracy.
Args:
a (int): Value to test for quadratic residuosity.
p (int): Positive modulus.
Returns:
bool: True if a is a quadratic residue modulo p, otherwise False.
"""
try:
return bool(sympy_is_quad_residue(a, p))
except Exception as e:
return f"Error: {str(e)}"Online Calculator
JACOBI
This function evaluates the Jacobi symbol (m/n) for an integer m and a positive odd integer n. It generalizes the Legendre symbol to composite odd moduli by multiplying Legendre symbols across the prime-power factors of n.
If n = \prod_i p_i^{\alpha_i}, then the Jacobi symbol is:
\left(\frac{m}{n}\right) = \prod_i \left(\frac{m}{p_i}\right)^{\alpha_i}
A result of -1 guarantees nonresiduosity modulo n, while a result of 1 does not always guarantee residuosity when n is composite.
Excel Usage
=JACOBI(m, n)
m(int, required): Numerator integer in the Jacobi symbol.n(int, required): Positive odd modulus.
Returns (int): Jacobi symbol value, typically -1, 0, or 1.
Example 1: Jacobi symbol returns negative one
Inputs:
| m | n |
|---|---|
| 45 | 77 |
Excel formula:
=JACOBI(45, 77)
Expected output:
-1
Example 2: Jacobi symbol returns positive one
Inputs:
| m | n |
|---|---|
| 60 | 121 |
Excel formula:
=JACOBI(60, 121)
Expected output:
1
Example 3: Jacobi symbol zero when numerator divisible by modulus factor
Inputs:
| m | n |
|---|---|
| 14 | 21 |
Excel formula:
=JACOBI(14, 21)
Expected output:
0
Example 4: Jacobi with coprime numerator and composite odd modulus
Inputs:
| m | n |
|---|---|
| 7 | 45 |
Excel formula:
=JACOBI(7, 45)
Expected output:
-1
Python Code
Show Code
from sympy import jacobi_symbol as sympy_jacobi_symbol
def jacobi(m, n):
"""
Compute the Jacobi symbol for two integers.
See: https://docs.sympy.org/latest/modules/ntheory.html#sympy.ntheory.residue_ntheory.jacobi_symbol
This example function is provided as-is without any representation of accuracy.
Args:
m (int): Numerator integer in the Jacobi symbol.
n (int): Positive odd modulus.
Returns:
int: Jacobi symbol value, typically -1, 0, or 1.
"""
try:
return int(sympy_jacobi_symbol(m, n))
except Exception as e:
return f"Error: {str(e)}"Online Calculator
LEGENDRE
This function evaluates the Legendre symbol (a/p) for an integer a and an odd prime p. It classifies whether a is a quadratic residue modulo p.
The value is defined by:
\left(\frac{a}{p}\right)= \begin{cases} 0, & p \mid a \\ 1, & a \text{ is a quadratic residue mod } p \\ -1, & a \text{ is a quadratic nonresidue mod } p \end{cases}
This test is fundamental in modular arithmetic and finite field computations.
Excel Usage
=LEGENDRE(a, p)
a(int, required): Integer whose residue class is tested.p(int, required): Odd prime modulus.
Returns (int): Legendre symbol value -1, 0, or 1.
Example 1: Legendre symbol zero case
Inputs:
| a | p |
|---|---|
| 0 | 7 |
Excel formula:
=LEGENDRE(0, 7)
Expected output:
0
Example 2: Legendre symbol residue case
Inputs:
| a | p |
|---|---|
| 2 | 7 |
Excel formula:
=LEGENDRE(2, 7)
Expected output:
1
Example 3: Legendre symbol nonresidue case
Inputs:
| a | p |
|---|---|
| 3 | 7 |
Excel formula:
=LEGENDRE(3, 7)
Expected output:
-1
Example 4: Equivalent residue classes produce same value
Inputs:
| a | p |
|---|---|
| 10 | 7 |
Excel formula:
=LEGENDRE(10, 7)
Expected output:
-1
Python Code
Show Code
from sympy import legendre_symbol as sympy_legendre_symbol
def legendre(a, p):
"""
Compute the Legendre symbol modulo an odd prime.
See: https://docs.sympy.org/latest/modules/ntheory.html#sympy.ntheory.residue_ntheory.legendre_symbol
This example function is provided as-is without any representation of accuracy.
Args:
a (int): Integer whose residue class is tested.
p (int): Odd prime modulus.
Returns:
int: Legendre symbol value -1, 0, or 1.
"""
try:
return int(sympy_legendre_symbol(a, p))
except Exception as e:
return f"Error: {str(e)}"Online Calculator
NORDER
This function returns the multiplicative order of a modulo n, defined as the smallest positive integer k such that a^k \equiv 1 \pmod n.
Formally:
\operatorname{ord}_n(a) = \min\{k \in \mathbb{N} : a^k \equiv 1 \pmod n\}
The order exists when \gcd(a,n)=1 and is central to cyclic subgroup structure in modular arithmetic.
Excel Usage
=NORDER(a, n)
a(int, required): Base integer in the modular multiplicative group.n(int, required): Modulus greater than one.
Returns (int): Smallest positive exponent yielding 1 modulo n.
Example 1: Multiplicative order of three modulo seven
Inputs:
| a | n |
|---|---|
| 3 | 7 |
Excel formula:
=NORDER(3, 7)
Expected output:
6
Example 2: Multiplicative order of four modulo seven
Inputs:
| a | n |
|---|---|
| 4 | 7 |
Excel formula:
=NORDER(4, 7)
Expected output:
3
Example 3: Multiplicative order in composite modulus
Inputs:
| a | n |
|---|---|
| 2 | 9 |
Excel formula:
=NORDER(2, 9)
Expected output:
6
Example 4: Multiplicative order with larger modulus
Inputs:
| a | n |
|---|---|
| 10 | 21 |
Excel formula:
=NORDER(10, 21)
Expected output:
6
Python Code
Show Code
from sympy import n_order as sympy_n_order
def norder(a, n):
"""
Find the multiplicative order of an integer modulo n.
See: https://docs.sympy.org/latest/modules/ntheory.html#sympy.ntheory.residue_ntheory.n_order
This example function is provided as-is without any representation of accuracy.
Args:
a (int): Base integer in the modular multiplicative group.
n (int): Modulus greater than one.
Returns:
int: Smallest positive exponent yielding 1 modulo n.
"""
try:
return int(sympy_n_order(a, n))
except Exception as e:
return f"Error: {str(e)}"Online Calculator
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
Show 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
PRIMROOT
This function returns a primitive root modulo p when the multiplicative group modulo p is cyclic.
A primitive root g modulo p generates all invertible residue classes, equivalently:
\operatorname{ord}_p(g) = \varphi(p)
Primitive roots exist only for moduli of the form 2, 4, q^e, or 2q^e where q is an odd prime.
Excel Usage
=PRIMROOT(p, smallest)
p(int, required): Modulus greater than one.smallest(bool, optional, default: true): If true, return the smallest primitive root.
Returns (int): A primitive root modulo p.
Example 1: Primitive root for a prime modulus
Inputs:
| p | smallest |
|---|---|
| 19 | true |
Excel formula:
=PRIMROOT(19, TRUE)
Expected output:
2
Example 2: Primitive root for another prime modulus
Inputs:
| p | smallest |
|---|---|
| 29 | true |
Excel formula:
=PRIMROOT(29, TRUE)
Expected output:
2
Example 3: Primitive root without smallest constraint
Inputs:
| p | smallest |
|---|---|
| 50 | false |
Excel formula:
=PRIMROOT(50, FALSE)
Expected output:
27
Example 4: Primitive root for power of two modulus
Inputs:
| p | smallest |
|---|---|
| 4 | true |
Excel formula:
=PRIMROOT(4, TRUE)
Expected output:
3
Python Code
Show Code
from sympy import primitive_root as sympy_primitive_root
def primroot(p, smallest=True):
"""
Find a primitive root modulo n when one exists.
See: https://docs.sympy.org/latest/modules/ntheory.html#sympy.ntheory.residue_ntheory.primitive_root
This example function is provided as-is without any representation of accuracy.
Args:
p (int): Modulus greater than one.
smallest (bool, optional): If true, return the smallest primitive root. Default is True.
Returns:
int: A primitive root modulo p.
"""
try:
root = sympy_primitive_root(p, smallest=smallest)
if root is None:
return "Error: No primitive root exists for the given modulus"
return int(root)
except Exception as e:
return f"Error: {str(e)}"Online Calculator
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
Show 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
QUADRES
This function returns all quadratic residues modulo p, i.e., all values attainable as squares in modular arithmetic.
The residue set is:
R_p = \{x^2 \bmod p : x \in \{0,1,\dots,p-1\}\}
The values are returned as a sorted single-column 2D array for Excel range output.
Excel Usage
=QUADRES(p)
p(int, required): Positive modulus.
Returns (list[list]): Single-column 2D array of quadratic residues modulo p.
Example 1: Quadratic residues modulo seven
Inputs:
| p |
|---|
| 7 |
Excel formula:
=QUADRES(7)
Expected output:
| Result |
|---|
| 0 |
| 1 |
| 2 |
| 4 |
Example 2: Quadratic residues modulo eleven
Inputs:
| p |
|---|
| 11 |
Excel formula:
=QUADRES(11)
Expected output:
| Result |
|---|
| 0 |
| 1 |
| 3 |
| 4 |
| 5 |
| 9 |
Example 3: Quadratic residues modulo nine
Inputs:
| p |
|---|
| 9 |
Excel formula:
=QUADRES(9)
Expected output:
| Result |
|---|
| 0 |
| 1 |
| 4 |
| 7 |
Example 4: Quadratic residues modulo thirteen
Inputs:
| p |
|---|
| 13 |
Excel formula:
=QUADRES(13)
Expected output:
| Result |
|---|
| 0 |
| 1 |
| 3 |
| 4 |
| 9 |
| 10 |
| 12 |
Python Code
Show Code
from sympy import quadratic_residues as sympy_quadratic_residues
def quadres(p):
"""
List all quadratic residues modulo p.
See: https://docs.sympy.org/latest/modules/ntheory.html#sympy.ntheory.residue_ntheory.quadratic_residues
This example function is provided as-is without any representation of accuracy.
Args:
p (int): Positive modulus.
Returns:
list[list]: Single-column 2D array of quadratic residues modulo p.
"""
try:
residues = sympy_quadratic_residues(p)
return [[int(v)] for v in residues]
except Exception as e:
return f"Error: {str(e)}"Online Calculator
SQRTMOD
This function finds solutions to the modular equation x^2 \equiv a \pmod p.
The congruence is:
x^2 \equiv a \pmod p
Depending on the modulus and value, there may be no solution, one solution, or multiple solutions. Results are returned as a single-column 2D array for Excel spill compatibility.
Excel Usage
=SQRTMOD(a, p, all_roots)
a(int, required): Right-hand-side residue value.p(int, required): Positive modulus.all_roots(bool, optional, default: true): If true, request all roots from the solver.
Returns (list[list]): Single-column 2D array of modular square roots, or a single blank cell if no roots exist.
Example 1: Single modular square root example
Inputs:
| a | p | all_roots |
|---|---|---|
| 11 | 43 | false |
Excel formula:
=SQRTMOD(11, 43, FALSE)
Expected output:
21
Example 2: All square roots for composite modulus
Inputs:
| a | p | all_roots |
|---|---|---|
| 17 | 32 | true |
Excel formula:
=SQRTMOD(17, 32, TRUE)
Expected output:
| Result |
|---|
| 7 |
| 9 |
| 23 |
| 25 |
Example 3: No modular square root returns blank cell
Inputs:
| a | p | all_roots |
|---|---|---|
| 3 | 7 | true |
Excel formula:
=SQRTMOD(3, 7, TRUE)
Expected output:
""
Example 4: All roots modulo an odd prime
Inputs:
| a | p | all_roots |
|---|---|---|
| 10 | 13 | true |
Excel formula:
=SQRTMOD(10, 13, TRUE)
Expected output:
| Result |
|---|
| 6 |
| 7 |
Python Code
Show Code
from sympy import sqrt_mod as sympy_sqrt_mod
def sqrtmod(a, p, all_roots=True):
"""
Solve quadratic congruences of the form x squared congruent to a.
See: https://docs.sympy.org/latest/modules/ntheory.html#sympy.ntheory.residue_ntheory.sqrt_mod
This example function is provided as-is without any representation of accuracy.
Args:
a (int): Right-hand-side residue value.
p (int): Positive modulus.
all_roots (bool, optional): If true, request all roots from the solver. Default is True.
Returns:
list[list]: Single-column 2D array of modular square roots, or a single blank cell if no roots exist.
"""
try:
roots = sympy_sqrt_mod(a, 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