https://github.com/byhill/modularsquareroots.jl
Modular square roots in Julia
https://github.com/byhill/modularsquareroots.jl
julia math modular-arithmetic square-root squareroot tonelli-shanks
Last synced: 3 months ago
JSON representation
Modular square roots in Julia
- Host: GitHub
- URL: https://github.com/byhill/modularsquareroots.jl
- Owner: byhill
- License: mit
- Created: 2023-01-20T21:08:32.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-09-09T22:22:47.000Z (9 months ago)
- Last Synced: 2025-02-21T04:46:11.239Z (4 months ago)
- Topics: julia, math, modular-arithmetic, square-root, squareroot, tonelli-shanks
- Language: Julia
- Homepage:
- Size: 38.1 KB
- Stars: 4
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ModularSquareRoots
[](https://ci.appveyor.com/project/byhill/ModularSquareRoots-jl)
## Overview
This module provides support for finding modular square-roots.
In particular, for a given integer $n$ and modulus $m$,
this module provides support for solving the congruence $x^2 \equiv n \pmod m$.To do this, you can use the function `sqrtmod(n, m)`.
```julia-repl
julia> using ModularSquareRootsjulia> sqrtmod(4, 5)
2-element Vector{Int64}:
3
2julia> all(powermod(x, 2, 5) == 4 for x in sqrtmod(4, 5))
truejulia> sqrtmod(1240, 289032)
8-element Vector{Int64}:
107056
251572
10712
155228
278320
133804
181976
37460julia> all(powermod(x, 2, 289032) == 1240 for x in sqrtmod(1240, 289032))
truejulia> sqrtmod(23, 200)
Int64[]julia> !any(powermod(x, 2, 200) == 23 for x in 0:199)
true
```## Prime Moduli
If you know that `p = m` is prime,
then you can additionally use the function `sqrtmodprime(n, p)`.
Note that there are no checks in `sqrtmodprime` to ensure that `p` is prime,
and the output of `sqrtmodprime(n, p)` is undefined when `p` is not prime.
The onus is on the user to use `sqrtmodprime` correctly.```julia-repl
julia> sqrtmodprime(16, 101)
2-element Vector{Int64}:
97
4julia> sqrtmodprime(15, 101)
Int64[]julia> sqrtmodprime(0, 101)
1-element Vector{Int64}:
0
```