9
Relevant links here and here, but here is the short version:
You have an input of two integers a and b between negative infinity and infinity (though if necessary, I can restrict the range, but the function must still accept negative inputs).
Definition of the Kronecker symbol
You must return the Kronecker symbol (a|b) for inputs a and b where
(a|b) = (a|p_1)^e_1 * (a|p_2)^e_2 * ... * (a|p_n)^e_n
where b = p_1^e_1 * p_2^e_2 * ... * p_n^e_n, and p_i and e_i are the primes and exponents in the prime factorization of b.
For an odd prime p, (a|p)=a^((p-1)/2) (mod p) as defined here.
For b == 2, (n|2)={0 for n even; 1 for n odd, n=+/-1 (mod 8); -1 for n odd, n=+/-3 (mod 8)
For b == -1, (n|-1)={-1 for n<0; 1 for n>0
If a >= b, (a|b) == (z|b) where z == a % b. By this property, and as explained here and here, a is a quadratic residue of b if z is, even though a >= b.
(-1|b) = 1 if b == 0,1,2 (mod 4) and -1 if b == 3 (mod 4). (0|b) is 0 except for (0|1) which is 1, because (a|1) is always 1 and for negative a, (-a|b) == (-1|b) * (a|b).
The output of the Kronecker symbol is always -1, 0 or 1, where the output is 0 if a and b have any common factors. If b is an odd prime, (a|b) == 1 if a is a quadratic residue mod b, and -1 if is it is not a quadratic residue.
Rules
Your code must be a program or a function.
The inputs must be in the order
a b.The output must be either
-1,0or1.This is code golf, so your code does not have to be efficient, just short.
No built-ins that directly calculate the Kronecker or the related Jacobi and Legendre symbols. Other built-ins (for prime factorization, for example) are fair game.
Examples
>>> kronecker(1, 5)
1
>>> kronecker(3, 8)
-1
>>> kronecker(15, 22)
1
>>> kronecker(21, 7)
0
>>> kronecker(5, 31)
1
>>> kronecker(31, 5)
1
>>> kronecker(7, 19)
1
>>> kronecker(19, 7)
-1
>>> kronecker(323, 455625)
1
>>> kronecker(0, 12)
0
>>> kronecker(0, 1)
1
>>> kronecker(12, 0)
0
>>> kronecker(1, 0)
1
>>> kronecker(-1, 5)
1
>>> kronecker(1, -5)
1
>>> kronecker(-1, -5)
-1
>>> kronecker(6, 7)
-1
>>> kronecker(-1, -7)
1
>>> kronecker(-6, -7)
-1
This is a complicated function, so please let me know if anything is unclear.



Are you sure you don't want to disallow built-ins? https://reference.wolfram.com/language/ref/KroneckerSymbol.html
– Martin Ender – 2015-11-30T18:54:13.650@MartinBüttner I was editing in examples when I saw your comment. I will disallow built-ins that directly calculate the Kronecker, Jacobi or Legendre symbols, but anything else (including prime factorization functions) should be fair game. – Sherlock9 – 2015-11-30T18:55:54.353
im not entirely sure why (31|5) gives 1. There shouldnt be a qudratic residue so why isnt it -1? – Eumel – 2015-11-30T19:51:06.683
also 7/19 should be 1 and 19/7 should be -1 according to the wiki you linked – Eumel – 2015-11-30T20:03:15.303
@Eumel 31 is a quadratic residue mod 5, because it is congruent to 1 mod 5. I have edited the question to explain this. – Sherlock9 – 2015-11-30T20:04:01.270
@Eumel Ah, you are correct. I'll edit the examples. – Sherlock9 – 2015-11-30T20:04:46.830
3If solutions have to handle negative and zero inputs correctly, you should definitely add some test cases for that. – Martin Ender – 2015-11-30T20:12:05.247
I think
kronecker(0, 12)is 0. – alephalpha – 2015-12-01T04:27:08.253I don't think you've actually defined the symbol for
a < 0. – Peter Taylor – 2015-12-09T00:12:21.553Half-way there. If you now define
(-1|b)I think the spec will be complete. – Peter Taylor – 2015-12-09T15:24:48.940Only very rarely does PPCG make me feel very, very stupid, as if I were browsing Math.SE, but this is one of those rare cases
– cat – 2015-12-19T20:52:04.620