Skip to content

Add Tonelli-Shanks algorithm for modular square roots - #15144

Open
Clear20-22 wants to merge 3 commits into
TheAlgorithms:masterfrom
Clear20-22:add-tonelli-shanks-algorithm
Open

Add Tonelli-Shanks algorithm for modular square roots#15144
Clear20-22 wants to merge 3 commits into
TheAlgorithms:masterfrom
Clear20-22:add-tonelli-shanks-algorithm

Conversation

@Clear20-22

Copy link
Copy Markdown
Contributor

Describe your change:

Added the Tonelli-Shanks Algorithm in maths/tonelli_shanks.py to compute modular square roots ($x^2 \equiv n \pmod p$) for an odd prime $p$. Also includes legendre_symbol calculation using Euler's criterion.

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

Copilot AI lite review requested due to automatic review settings September 1, 2026 07:18
@algorithms-keeper algorithms-keeper Bot added the require descriptive names This PR needs descriptive function and/or variable names label Sep 1, 2026

@algorithms-keeper algorithms-keeper Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

Comment thread maths/tonelli_shanks.py Outdated
from __future__ import annotations


def legendre_symbol(n: int, p: int) -> int:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: n

Please provide descriptive name for the parameter: p

Comment thread maths/tonelli_shanks.py Outdated
return ls if ls <= 1 else -1


def tonelli_shanks(n: int, p: int) -> tuple[int, int]:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: n

Please provide descriptive name for the parameter: p

@algorithms-keeper algorithms-keeper Bot added the awaiting reviews This PR is ready to be reviewed label Sep 1, 2026

@algorithms-keeper algorithms-keeper Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

Comment thread maths/tonelli_shanks.py Outdated
from __future__ import annotations


def legendre_symbol(n: int, p: int) -> int:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: n

Please provide descriptive name for the parameter: p

Comment thread maths/tonelli_shanks.py Outdated
return ls if ls <= 1 else -1


def tonelli_shanks(n: int, p: int) -> tuple[int, int]:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: n

Please provide descriptive name for the parameter: p

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a new educational implementation of the Tonelli–Shanks algorithm to compute modular square roots (x^2 \equiv n \pmod p) for an odd prime modulus, along with a helper for computing the Legendre symbol via Euler’s criterion.

Changes:

  • Added legendre_symbol() helper using Euler’s criterion.
  • Added tonelli_shanks() implementation with doctests and references for modular square roots modulo an odd prime.
Suppressed comments (2)

maths/tonelli_shanks.py:86

  • The error message and doctests claim p is validated as an odd prime, but the code only rejects even/<=2 values. Consider rewording the message/doctests to avoid implying primality is checked (or add a prime check).
    if p <= 2 or p % 2 == 0:
        msg = f"Modulus p must be an odd prime (got {p})."
        raise ValueError(msg)

maths/tonelli_shanks.py:112

  • The loop that searches for a quadratic non-residue (z) is unbounded. Since p is not actually validated as prime, invalid inputs can cause this to run indefinitely. Add a bound (z < p) and fail fast with a helpful error if no non-residue is found.
    # Case 2: Search for a quadratic non-residue z modulo p
    z = 2
    while legendre_symbol(z, p) != -1:
        z += 1


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread maths/tonelli_shanks.py
Comment on lines +57 to +60
Raises:
ValueError: If p is not an odd prime >= 3.
ValueError: If n is not a quadratic residue modulo p.

@algorithms-keeper algorithms-keeper Bot removed the require descriptive names This PR needs descriptive function and/or variable names label Sep 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants