def aks(n): if n % 2 == 0: return n == 2 if perfect_power(n) != False: return False r = 2 l2n = log(n, 2).n() ordx = order(r, n) while ordx <= l2n**2: r += 1 # SageMath has built-in GCD if gcd(r, n) != 1: if r < n: return False else: continue ordx = order(r, n) if n <= r: return True # This is where SageMath is important zn = PolynomialRing(Integers(n), 'v') v = zn.gen() znbtr = zn.quotient(v ^ r - 1, 'x') x = znbtr.gen() for a in range(1, floor(sqrt(euler_phi(r)) * l2n)): if (x + a) ^ n != (x ^ n + a): return False return True
def mu(n): # check if the value is memoized check = memo.get(n) if check is not None: return memo[n] # sympy.perfect_power returns (base, exp) or False tmp = perfect_power(n) if tmp: # if n is a prime power not pre-memoized, then exp > base, and we use kempner's algorithm from [1] if isprime(tmp[0]): return (tmp[0] - 1) * tmp[1] + sumk(tmp[0], tmp[1]) # sympy.factorint returns a dictionary of { primefactor : power } # we recursively check the value of mu(primefactor**power), returning the max # this should be very fast most of the time, as small primes have all been pre-memoized else: f = factorint(n) memo[n] = max([mu(p**f[p]) for p in f.keys()]) return memo[n] else: # any prime not pre-memoized : mu(p) = p if isprime(n): memo[n] = n return memo[n] # else recursively check the prime powers in n's factorization as above f = factorint(n) memo[n] = max([mu(p**f[p]) for p in f.keys()]) return memo[n]
def check_q(self, q: int) -> None: """Assert whether q is prime power. """ assert q % 4 == 1, '{} != 1 mod 4'.format(q) if not sympy.isprime(q): split_power = sympy.perfect_power(q) assert split_power, '{} is not a power'.format(q) p, _ = split_power assert sympy.isprime(p), '{} is not prime'.format(p)
def int_sqrt(n): # int if n < 0: return None if n < 2: return n be = sy.perfect_power(n, candidates=[2]) if be is False: return None base, exp = be assert exp == 2 return base
def get_primes_and_pp(number_from, number_to): result = 0 print('======================================') print(number_from) print(number_to) print('======================================') while number_from <= number_to: if(sympy.perfect_power(number_from)): result += 1 if(isPrime(number_from)): result += 1 number_from += 1 return result
def get_primes_and_pp(number_from, number_to): result = 0 print('======================================') print(number_from) print(number_to) print('======================================') while number_from <= number_to: if (sympy.perfect_power(number_from)): result += 1 if (isPrime(number_from)): result += 1 number_from += 1 return result
def _eval_power(b, e): """ Tries to do some simplifications on b ** e, where b is an instance of Integer Returns None if no further simplifications can be done When exponent is a fraction (so we have for example a square root), we try to find a simpler representation by factoring the argument up to factors of 2**15, e.g. - 4**Rational(1,2) becomes 2 - (-4)**Rational(1,2) becomes 2*I - (2**(3+7)*3**(6+7))**Rational(1,7) becomes 6*18**(3/7) Further simplification would require a special call to factorint on the argument which is not done here for sake of speed. """ from sympy import perfect_power if e is S.NaN: return S.NaN if b is S.One: return S.One if b is S.NegativeOne: return if e is S.Infinity: if b > S.One: return S.Infinity if b is S.NegativeOne: return S.NaN # cases for 0 and 1 are done in their respective classes return S.Infinity + S.ImaginaryUnit * S.Infinity if not isinstance(e, Number): # simplify when exp is even # (-2) ** k --> 2 ** k c, t = b.as_coeff_mul() if e.is_even and isinstance(c, Number) and c < 0: return (-c*Mul(*t))**e if not isinstance(e, Rational): return if e is S.Half and b < 0: # we extract I for this special case since everyone is doing so return S.ImaginaryUnit*Pow(-b, e) if e < 0: # invert base and change sign on exponent ne = -e if b < 0: if e.q != 1: return -(S.NegativeOne)**((e.p % e.q) / S(e.q)) * Rational(1, -b)**ne else: return (S.NegativeOne)**ne*Rational(1, -b)**ne else: return Rational(1, b)**ne # see if base is a perfect root, sqrt(4) --> 2 b_pos = int(abs(b)) x, xexact = integer_nthroot(b_pos, e.q) if xexact: # if it's a perfect root we've finished result = Integer(x ** abs(e.p)) if b < 0: result *= (-1)**e return result # The following is an algorithm where we collect perfect roots # from the factors of base. # if it's not an nth root, it still might be a perfect power p = perfect_power(b_pos) if p: dict = {p[0]: p[1]} else: dict = Integer(b_pos).factors(limit=2**15) # now process the dict of factors if b.is_negative: dict[-1] = 1 out_int = 1 sqr_int = 1 sqr_gcd = 0 sqr_dict = {} for prime, exponent in dict.iteritems(): exponent *= e.p div_e, div_m = divmod(exponent, e.q) if div_e > 0: out_int *= prime**div_e if div_m > 0: sqr_dict[prime] = div_m for p, ex in sqr_dict.iteritems(): if sqr_gcd == 0: sqr_gcd = ex else: sqr_gcd = igcd(sqr_gcd, ex) for k, v in sqr_dict.iteritems(): sqr_int *= k**(v//sqr_gcd) if sqr_int == b and out_int == 1: result = None else: result = out_int*Pow(sqr_int , Rational(sqr_gcd, e.q)) return result
def isPerfectPower(n): if perfect_power(n)==False : return False; return True;
def n(self): if sympy.isprime(self.q): return 1 else: return sympy.perfect_power(self.q)[1]
def p(self): if sympy.isprime(self.q): return self.q else: return sympy.perfect_power(self.q)[0]