|
4 | 4 | # Note: Version 0.7 changes some function names to align with SAGE
|
5 | 5 | # Note: Version 0.8 is compatible with both Python 2.5+ and 3.x
|
6 | 6 | # Author: Robert Campbell, <r.campbel.256@gmail.com>
|
7 |
| -# Date: 17 July, 2019 |
8 |
| -# Version 0.83 |
| 7 | +# Contributors: Ege Alpay; ZhiHang Fan |
| 8 | +# Date: 13 Oct, 2019 |
| 9 | +# Version 0.84 |
9 | 10 | # License: Simplified BSD (see details at bottom)
|
10 | 11 | ######################################################################################
|
11 | 12 | """Basic number theory functions.
|
|
15 | 16 | power_mod(b,e,n) - Compute b^e mod n efficiently.
|
16 | 17 | inverse_mod(b,n) - Compute 1/b mod n.
|
17 | 18 | is_prime(n) - Test whether n is prime using a variety of pseudoprime tests.
|
| 19 | + euler_criterion(a, p) - Test whether a is a quadratic residue mod p |
18 | 20 | euler_phi(n) - Compute Euler's Phi function of n - the number of integers strictly less than n which are coprime to n.
|
19 | 21 | carmichael_lambda(n) - Compute Carmichael's Lambda function of n - the smallest exponent e such that b**e = 1 for all b coprime to n.
|
20 | 22 | factor(n) - Return a sorted list of the prime factors of n with exponents.
|
|
37 | 39 | isprimitive(g,n) - Test whether g is primitive mod n. (Renamed is_primitive_root(g,n) in ver 0.8)
|
38 | 40 | """
|
39 | 41 |
|
40 |
| -__version__ = '0.83' # Format specified in Python PEP 396 |
41 |
| -Version = 'NUMBTHY.PY, version ' + __version__ + ', 17 July, 2019, by Robert Campbell, <r.campbel.256@gmail.com>' |
| 42 | +__version__ = '0.84' # Format specified in Python PEP 396 |
| 43 | +Version = 'NUMBTHY.PY, version ' + __version__ + ', 13 Oct, 2019, by Robert Campbell, <r.campbel.256@gmail.com>' |
42 | 44 |
|
43 | 45 | import math # Use sqrt, floor
|
44 | 46 | import functools # Use reduce (Python 2.5+ and 3.x)
|
45 | 47 |
|
46 | 48 | def euler_criterion(a, p):
|
47 |
| - """p is odd prime, a is positive integer. Euler's Criterion will check if a is QR mod p. If yes, returns True. If a is NR mod p, then False""" |
48 |
| - return a ** ((p - 1) / 2) % p == 1 |
| 49 | + """p is odd prime, a is positive integer. Euler's Criterion will check if |
| 50 | + a is a quadratic residue mod p. If yes, returns True. If a is a non-residue |
| 51 | + mod p, then False""" |
| 52 | + return pow(a, (p - 1) // 2, p) == 1 |
49 | 53 |
|
50 | 54 | def gcd(a,b):
|
51 | 55 | """gcd(a,b) returns the greatest common divisor of the integers a and b."""
|
@@ -109,6 +113,7 @@ def factor(n):
|
109 | 113 | if ((abs(n) == 1) or (n == 0)): raise ValueError('Unable to factor {0}'.format(n))
|
110 | 114 | factspow = []
|
111 | 115 | currfact = None
|
| 116 | + thecount = 1 |
112 | 117 | for thefact in factors(n):
|
113 | 118 | if thefact != currfact:
|
114 | 119 | if currfact != None:
|
@@ -260,9 +265,9 @@ def invmod(b,n):
|
260 | 265 | return inverse_mod(b,n)
|
261 | 266 |
|
262 | 267 | def eulerphi(n):
|
263 |
| - """eulerphi(n) - Compute Euler's Phi function of n - the number of integers strictly less than n which are coprime to n. |
264 |
| - (Renamed euler_phi(n) in ver 0.7)""" |
265 |
| - return euler_phi(n) |
| 268 | + """eulerphi(n) - Compute Euler's Phi function of n - the number of integers strictly less than n which are coprime to n. |
| 269 | + (Renamed euler_phi(n) in ver 0.7)""" |
| 270 | + return euler_phi(n) |
266 | 271 |
|
267 | 272 | def carmichaellambda(n):
|
268 | 273 | """carmichaellambda(n) - Compute Carmichael's Lambda function
|
@@ -310,4 +315,3 @@ def isprimitive(g,n):
|
310 | 315 | # notice, this list of conditions and the following disclaimer in
|
311 | 316 | # the documentation and/or other materials provided with the distribution.
|
312 | 317 | ############################################################################
|
313 |
| - |
|
0 commit comments