Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A faster totient function #1086

Closed
wants to merge 7 commits into from
Closed
2 changes: 0 additions & 2 deletions doc/src/modules/ntheory.txt
Expand Up @@ -89,8 +89,6 @@ Ntheory Functions Reference

.. autofunction:: int_tested

.. autofunction:: totient_

.. autofunction:: n_order

.. autofunction:: is_primitive_root
Expand Down
2 changes: 1 addition & 1 deletion sympy/ntheory/__init__.py
Expand Up @@ -9,6 +9,6 @@
pollard_pm1, pollard_rho, primefactors, totient, trailing, divisor_count
from partitions_ import npartitions
from residue_ntheory import is_primitive_root, is_quad_residue, \
legendre_symbol, jacobi_symbol, n_order, totient_
legendre_symbol, jacobi_symbol, n_order
from multinomial import binomial_coefficients, binomial_coefficients_list,\
multinomial_coefficients
28 changes: 3 additions & 25 deletions sympy/ntheory/residue_ntheory.py
@@ -1,6 +1,6 @@
from sympy.core.numbers import igcd
from primetest import isprime
from factor_ import factorint, trailing
from factor_ import factorint, trailing, totient

def int_tested(*j):
"""Return all args as integers after confirming that they are integers.
Expand All @@ -19,28 +19,6 @@ def int_tested(*j):
return i[0]
return i

def totient_(n):
"""Returns the number of integers less than n and relatively prime to n.

Examples
========

>>> from sympy.ntheory import totient_
>>> totient_(6)
2
>>> totient_(67)
66

"""
n = int_tested(n)
if n < 1:
raise ValueError("n must be a positive integer")
tot = 0
for x in xrange(1, n):
if igcd(x, n) == 1:
tot += 1
return tot


def n_order(a, n):
"""Returns the order of a modulo n
Expand All @@ -59,7 +37,7 @@ def n_order(a, n):
a, n = int_tested(a, n)
if igcd(a, n) != 1:
raise ValueError("The two numbers should be relatively prime")
group_order = totient_(n)
group_order = totient(n)
factors = factorint(group_order)
order = 1
if a > n:
Expand Down Expand Up @@ -102,7 +80,7 @@ def is_primitive_root(a, p):
raise ValueError("The two numbers should be relatively prime")
if a > p:
a = a % p
if n_order(a, p) == totient_(p):
if n_order(a, p) == totient(p):
return True
else:
return False
Expand Down