Legendre symbol : legendre_symbol

### 4.6.26 Legendre symbol : legendre_symbol

If n is prime, we define the Legendre symbol of a written (a/n) by :

⎛ ⎜ ⎜ ⎝ |
a
n
| ⎞ ⎟ ⎟ ⎠ | = | ⎧ ⎪ ⎨ ⎪ ⎩ |
0 | if a=0 modn
1 | if a ≠ 0 modn and if a=b2 modn
−1 | if a ≠ 0 modn and if a ≠ b2 modn

Some properties

- If n is prime :

  a |
  n−1
  2
  | = | ⎛ ⎜ ⎜ ⎝ |
  a
  n
  | ⎞ ⎟ ⎟ ⎠ | modn

- ⎛ ⎜ ⎜ ⎝ |
  p
  q
  | ⎞ ⎟ ⎟ ⎠ | . | ⎛ ⎜ ⎜ ⎝ |
  q
  p
  | ⎞ ⎟ ⎟ ⎠
  | = |
  (−1) |
  p−1
  2
  | .(−1) |
  q−1
  2
  | if p and q are odd and positive
  ⎛ ⎜ ⎜ ⎝ |
  2
  p
  | ⎞ ⎟ ⎟ ⎠
  | = |
  (−1) |
  p2−1
  8
  ⎛ ⎜ ⎜ ⎝ |
  −1
  p
  | ⎞ ⎟ ⎟ ⎠
  | = |
  (−1) |
  p−1
  2

legendre_symbol takes two arguments a and n and returns the Legendre symbol (a/n).
Input :

legendre_symbol(26,17)

Output :

1

Input :

legendre_symbol(27,17)

Output :

-1

Input :

legendre_symbol(34,17)

Output :

0