# Using JacobiSymbol vs KroneckerSymbol

**URL:** <https://community.wolfram.com/t/using-jacobisymbol-vs-kroneckersymbol/24017>\
**Category:** Questions\
**Tags:** discrete-mathematics\
**Created:** [May 21, 2024, 8:53am UTC](https://community.wolfram.com/t/using-jacobisymbol-vs-kroneckersymbol/24017 "2024-05-21T08:53:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pxausa](https://avatars.discourse-cdn.com/v4/letter/p/7cd45c/32.png) [@pxausa](https://community.wolfram.com/u/pxausa)\
**Post date:** [May 21, 2024, 8:53am UTC](https://community.wolfram.com/t/using-jacobisymbol-vs-kroneckersymbol/24017/1 "2024-05-21T08:53:52Z")

</div>

Hello all,

from my understanding, both JacobiSymbol(n,m) and KroneckerSymbol(n,m) return the same values when their arguments are integer, even though JacobiSymbol is about 6 X faster (and even though, mathematically, the Jacobi symbol should only be defined for odd positive m).

```
nn=1000;

a=Table[KroneckerSymbol[n,m], {n, -nn, nn},{m,-nn,nn}]; // AbsoluteTiming

b=Table[JacobiSymbol[n,m], {n, -nn, nn},{m,-nn,nn}]; // AbsoluteTiming

a==b (* True *)

```

So, is it safe to use JacobiSymbol instead of KroneckerSymbol?

Thank you in advance.

---

<div class="post-metadata">

**Author:** ![mariuszi28cg](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/mariuszi28cg/32/308_2.png) [@mariuszi28cg](https://community.wolfram.com/u/mariuszi28cg)\
**Post date:** [May 21, 2024, 9:20am UTC](https://community.wolfram.com/t/using-jacobisymbol-vs-kroneckersymbol/24017/2 "2024-05-21T09:20:46Z")

</div>

From help pages of **KroneckerSymbol** :

> KroneckerSymbol is the generalization of the Jacobi symbol for all  
> integers

```
  jacobi[n_, m_] := KroneckerSymbol[n, m] /; (OddQ[m] && Positive[m]);
  Table[jacobi[2, m], {m, 1, 9}]

  (*{1, jacobi[2, 2], -1, jacobi[2, 4], -1, jacobi[2, 6], 1, jacobi[2, 8], 1}*)

```

Regards M.I.

---

<div class="post-metadata">

**Author:** ![pxausa](https://avatars.discourse-cdn.com/v4/letter/p/7cd45c/32.png) [@pxausa](https://community.wolfram.com/u/pxausa)\
**Post date:** [May 21, 2024, 9:24am UTC](https://community.wolfram.com/t/using-jacobisymbol-vs-kroneckersymbol/24017/3 "2024-05-21T09:24:11Z")

</div>

Yes, thank you, but JacobiSymbol[2,4] (for example) works and returns the correct value:

```
JacobiSymbol[2,4] == KroneckerSymbol[2,4]

```

I can’t find any difference between the two functions (with the added bonus that JacobiSymbol is faster).

---

<div class="post-metadata">

**Author:** ![mariuszi28cg](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/mariuszi28cg/32/308_2.png) [@mariuszi28cg](https://community.wolfram.com/u/mariuszi28cg)\
**Post date:** [May 21, 2024, 9:12pm UTC](https://community.wolfram.com/t/using-jacobisymbol-vs-kroneckersymbol/24017/4 "2024-05-21T21:12:41Z")

</div>

**ChatGPT AI says:**

Yes, in the Wolfram Language, it is generally safe to use `JacobiSymbol` instead of `KroneckerSymbol` when dealing with integer arguments, with a few considerations:

1. **Definition and Generality** :

2. **Performance** :

3. **Practical Equivalence** :

4. **Specific Cases** :

### Conclusion

Given that:

- `JacobiSymbol` and `KroneckerSymbol` produce the same results for all integer arguments in the Wolfram Language.
- `JacobiSymbol` is significantly faster.

It is safe and beneficial to use `JacobiSymbol` in place of `KroneckerSymbol` for integer arguments, leveraging the improved performance without losing correctness. However, always ensure your specific use cases align with the described behavior and test thoroughly if you are implementing this in critical applications.

**Gemini AI says:**

You’re right! In Wolfram Language, `JacobiSymbol(n, m)` can be a faster alternative to `KroneckerSymbol(n, m)` for integer arguments, even though they might seem like different functions. Here’s the breakdown:

- **Kronecker Symbol (δ)**: This symbol is simply 1 if n and m are equal and 0 otherwise. It works for any integer arguments.
- **Jacobi Symbol (())**: This symbol deals with residues modulo an odd positive integer (m). It returns 1 if n has a square root modulo m, -1 if it doesn’t, and 0 if n is divisible by m.

Now, for integers:

- When m is odd and positive, both symbols essentially check the same thing (square root modulo m). This is why `JacobiSymbol` is faster - it’s specifically designed for this case.
- When m is even or negative, the Jacobi symbol isn’t mathematically defined. However, Wolfram Language seems to cleverly extend the behavior of the Jacobi symbol for these cases to match the Kronecker symbol (always 0 for even m and 1 for negative m).

**So, is it safe to use JacobiSymbol?**

Yes, in most cases, it’s safe to use `JacobiSymbol` instead of `KroneckerSymbol` if you’re only dealing with integers for n and m. It’s generally faster and achieves the same result.

**However, there are a few caveats:**

- **Clarity:** If your code needs to be mathematically rigorous or emphasizes the concept of divisibility (Kronecker symbol), using `KroneckerSymbol` might be clearer.
- **Non-Integer Arguments:** If you plan to use non-integer arguments later, stick with `KroneckerSymbol` as it’s defined for a wider range.

**Summary:**

For pure integer computations, `JacobiSymbol` is a good choice for speed. Just be aware of the mathematical definition limitations and use `KroneckerSymbol` if clarity or broader applicability is needed.
