Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
21
Task/Jacobi-symbol/Python/jacobi-symbol.py
Normal file
21
Task/Jacobi-symbol/Python/jacobi-symbol.py
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
def jacobi(a, n):
|
||||
if n <= 0:
|
||||
raise ValueError("'n' must be a positive integer.")
|
||||
if n % 2 == 0:
|
||||
raise ValueError("'n' must be odd.")
|
||||
a %= n
|
||||
result = 1
|
||||
while a != 0:
|
||||
while a % 2 == 0:
|
||||
a /= 2
|
||||
n_mod_8 = n % 8
|
||||
if n_mod_8 in (3, 5):
|
||||
result = -result
|
||||
a, n = n, a
|
||||
if a % 4 == 3 and n % 4 == 3:
|
||||
result = -result
|
||||
a %= n
|
||||
if n == 1:
|
||||
return result
|
||||
else:
|
||||
return 0
|
||||
Loading…
Add table
Add a link
Reference in a new issue