1 from sympy
.core
.numbers
import gcd
2 from primetest
import isprime
5 """returns the number of integers less than n
6 and relatively prime to n"""
8 raise ValueError, "n must be a positive integer"
16 """ returns the order of a modulo n
17 Order of a modulo n is the smallest integer
18 k such that a^k leaves a remainder of 1 with n.
22 for x
in xrange(1,totient_(n
)+1):
26 def is_primitive_root(a
,p
):
28 returns True if a is a primitive root of p
30 assert gcd(a
,p
) == 1,"The two numbers should be relatively prime"
33 if n_order(a
,p
)==totient_(p
):
38 def is_quad_residue(a
,p
):
40 returns True if a is a quadratic residue of p
41 p should be a prime and a should be relatively
44 assert isprime(p
) and p
!=2,"p should be an odd prime"
45 assert gcd(a
,p
)==1,"The two numbers should be relatively prime"
48 rem
=(a
**((p
-1)/2))%p
# a^(p-1 / 2) % p
49 if rem
==1: return True
52 def legendre_symbol(a
,p
):
54 return 1 if a is a quadratic residue of p
56 p should be an odd prime by definition
58 assert isprime(p
) and p
!=2,"p should be an odd prime"
59 assert gcd(a
,p
)==1,"The two numbers should be relatively prime"
62 if is_quad_residue(a
,p
)==True: return 1