Home c++ Calculation of the Jacobi symbol

# Calculation of the Jacobi symbol

Author

Date

Category

It is impossible to write the right implementation of the Jacobi JCA (Q / P) symbol calculation algorithm. First, the algorithm itself:

Login: `q `, `p `– integers.

Exit: Jackobi symbol value.

1) s = 0, u = q, v = p.

2) Calculate `R `– the smallest positive residue during the division `u `on `v `. Calculate the whole `k & gt; = 0 `and odd `t `, such as `r = t * 2 ^ k `. Calculate `s = s + k * (V ^ 2 – 1) / 8 + (t – 1) * (V – 1) / 4 (mod 2)

3) If `T = 1 `, then the Jacobi symbol is `(- 1) ^ s `. End.

4) if `t & gt; = 3 `, then `u = V `, `v = t `, go to step 2.

Sales:

``````# include & lt; iostream & gt;
int jacobi (int q, int p)
{
int s = 0, u = q, v = p;
int r, k, t;
do {
// Calculate R - the smallest positive residue when dividing U on V
r = u% v;
// Calculate the integer k & gt; = 0 and odd T: R = T * 2 ^ K
k = t = 0;
While (R% 2 == 0)
{
k ++; // Indicator Degree deception among R
R & GT; & GT; = 1; // Delim R to 2
}
t = r; // in T is the result of dividing R to 2 ^ k
S = (S + K * (V * V - 1) / 8 + (T - 1) * (V - 1) / 4)% 2;
if (t == 1)
Return (s)? eleven;
// New iteration
U = V;
V = T;
} While (T & GT; = 3);
}
INT MAIN () {
STD :: COUT & LT; & LT; Jacobi (-104, 997) & lt; & lt; STD :: ENDL;
Return 0;
}
``````

here j (-104, 997) should be equal to -1, and my implementation issues 1.

Tests from Help Maple: `Jacobi (12, 3) = 0 `, `jacobi (28, 21) = 0 `, `Jacobi (6, 11) = -1 `, `Jacobi (226, 135) = 1 `, `jacobi (26, 35) = -1 `, `Jacobi (-286, 4272943 ) = 1 `, `Jacobi (888, 1999) = -1 `.

Supplement: In ternary terms, I was wrong. If s == 1 (true), then you need to return -1, because the indicator of the degree is odd.

Maybe mathematics will complement something?

And let’s Book open.

### Jacobi symbol properties

for odd p & gt; 1 :

1. j (a, p) = j (a% p, p)
2. j (1, p) = 1
3. j (-1, p) = (-1) (P-1) / 2
4. j (ab..l, p) = j (a, p) j (b, p) … j (l, p) , reference: j (ab 2 , p) = j (a, p)
5. j (2, p) = (-1) (p 2 – 1) / 8

for Positive odd mutually simple p and q :

1. j (q, p) = (-1) (P – 1) (Q-1) / 4 J (P, Q) .

Neither in the algorithm, nor in the program, I do not even see hints of any analysis of signs or factorization.
Those. The main used formula looks unproved .

Also known that the Jacobi symbol J (A, P) is +1 or -1 , because is defined For all a , not dividing on p . Therefore, zero The result in the test Maple needs a special interpretation – for example, for J (12.3) .

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.