Skip to content

Commit 1f9b324

Browse files
20191013 - ver 0.84: Correct bug in factor(). Added use of pow() and minor fiddly format changes to euler_criterion() contribution (thanks Ege Alpay & ZhiHang Fan).
1 parent 898f6b0 commit 1f9b324

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

numbthy.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
# Note: Version 0.7 changes some function names to align with SAGE
55
# Note: Version 0.8 is compatible with both Python 2.5+ and 3.x
66
# 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
910
# License: Simplified BSD (see details at bottom)
1011
######################################################################################
1112
"""Basic number theory functions.
@@ -15,6 +16,7 @@
1516
power_mod(b,e,n) - Compute b^e mod n efficiently.
1617
inverse_mod(b,n) - Compute 1/b mod n.
1718
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
1820
euler_phi(n) - Compute Euler's Phi function of n - the number of integers strictly less than n which are coprime to n.
1921
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.
2022
factor(n) - Return a sorted list of the prime factors of n with exponents.
@@ -37,15 +39,17 @@
3739
isprimitive(g,n) - Test whether g is primitive mod n. (Renamed is_primitive_root(g,n) in ver 0.8)
3840
"""
3941

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>'
4244

4345
import math # Use sqrt, floor
4446
import functools # Use reduce (Python 2.5+ and 3.x)
4547

4648
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
4953

5054
def gcd(a,b):
5155
"""gcd(a,b) returns the greatest common divisor of the integers a and b."""
@@ -109,6 +113,7 @@ def factor(n):
109113
if ((abs(n) == 1) or (n == 0)): raise ValueError('Unable to factor {0}'.format(n))
110114
factspow = []
111115
currfact = None
116+
thecount = 1
112117
for thefact in factors(n):
113118
if thefact != currfact:
114119
if currfact != None:
@@ -260,9 +265,9 @@ def invmod(b,n):
260265
return inverse_mod(b,n)
261266

262267
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)
266271

267272
def carmichaellambda(n):
268273
"""carmichaellambda(n) - Compute Carmichael's Lambda function
@@ -310,4 +315,3 @@ def isprimitive(g,n):
310315
# notice, this list of conditions and the following disclaimer in
311316
# the documentation and/or other materials provided with the distribution.
312317
############################################################################
313-

0 commit comments

Comments
 (0)