function isPrime(n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 === 0 || n % 3 === 0) return false;
for (var i = 5; i * i 0n) {
if (exp % 2n === 1n) res = (res * base) % m;
base = (base * base) % m;
exp = exp / 2n;
}
return res;
}
function calculateLegendre() {
var aInput = document.getElementById("integerA").value;
var pInput = document.getElementById("primeP").value;
var errorDiv = document.getElementById("primeError");
var resultBox = document.getElementById("resultBox");
var resultText = document.getElementById("legendreResult");
var interpretation = document.getElementById("interpretation");
errorDiv.style.display = "none";
resultBox.style.display = "none";
if (aInput === "" || pInput === "") {
alert("Please fill in both fields.");
return;
}
var a = parseInt(aInput);
var p = parseInt(pInput);
if (p <= 2 || p % 2 === 0 || !isPrime(p)) {
errorDiv.style.display = "block";
return;
}
var result;
var aModP = ((a % p) + p) % p;
if (aModP === 0) {
result = 0;
} else {
var exponent = (p – 1) / 2;
var res = power(aModP, exponent, p);
if (res === 1n) {
result = 1;
} else {
result = -1;
}
}
resultText.innerHTML = "(" + a + " / " + p + ") = " + result;
var text = "";
if (result === 1) {
text = a + " is a quadratic residue modulo " + p;
} else if (result === -1) {
text = a + " is a quadratic non-residue modulo " + p;
} else {
text = a + " is a multiple of " + p;
}
interpretation.innerHTML = text;
resultBox.style.display = "block";
}
Understanding the Legendre Symbol
In number theory, the Legendre symbol is a function that indicates whether a given integer a is a quadratic residue modulo an odd prime p. This mathematical tool is fundamental in modular arithmetic and is a precursor to the Jacobi symbol and the Kronecker symbol.
What is a Quadratic Residue?
An integer a is called a quadratic residue modulo p if there exists some integer x such that:
x² ≡ a (mod p)
If no such x exists, and a is not divisible by p, then a is called a quadratic non-residue modulo p.
Mathematical Values of the Legendre Symbol (a/p)
(a/p) = 1: If a is a quadratic residue modulo p (and a is not 0 mod p).
(a/p) = -1: If a is a quadratic non-residue modulo p.
(a/p) = 0: If a is a multiple of p (i.e., a ≡ 0 mod p).
Calculation Method: Euler's Criterion
This calculator utilizes Euler's Criterion to determine the value. The formula states:
(a/p) ≡ a(p-1)/2 (mod p)
This means we raise the integer a to the power of half the prime minus one, then find the remainder when divided by p. If the result is 1, the symbol is 1. If the result is p-1 (which is -1 mod p), the symbol is -1.
Properties of the Legendre Symbol
The Legendre symbol follows several useful algebraic properties:
Completely Multiplicative: (ab/p) = (a/p)(b/p).
Periodicity: If a ≡ b (mod p), then (a/p) = (b/p).
Quadratic Reciprocity: For two distinct odd primes p and q, (p/q)(q/p) = (-1)((p-1)/2)((q-1)/2).
Practical Example
Let's calculate (3 / 7):
Identify a = 3 and p = 7.
Apply Euler's Criterion: 3(7-1)/2 = 3³ = 27.
Find 27 mod 7: 27 = (3 × 7) + 6. So 27 ≡ 6 mod 7.
Since 6 ≡ -1 mod 7, the result is -1.
Conclusion: 3 is a quadratic non-residue modulo 7 (there is no integer x where x² ≡ 3 mod 7).