#NEWLINE# def perfect_power( n , candidates = None , big = True , factor = True ) : #NEWLINE# #INDENT# '\n    Return ``(b, e)`` such that ``n`` == ``b**e`` if ``n`` is a\n    perfect power; otherwise return ``False``.\n\n    By default, the base is recursively decomposed and the exponents\n    collected so the largest possible ``e`` is sought. If ``big=False``\n    then the smallest possible ``e`` (thus prime) will be chosen.\n\n    If ``candidates`` for exponents are given, they are assumed to be sorted\n    and the first one that is larger than the computed maximum will signal\n    failure for the routine.\n\n    If ``factor=True`` then simultaneous factorization of n is attempted\n    since finding a factor indicates the only possible root for n. This\n    is True by default since only a few small factors will be tested in\n    the course of searching for the perfect power.\n\n    Examples\n    ========\n\n    >>> from sympy import perfect_power\n    >>> perfect_power(16)\n    (2, 4)\n    >>> perfect_power(16, big = False)\n    (4, 2)\n    ' #NEWLINE# n = int ( n ) #NEWLINE# if ( n < 3 ) : #NEWLINE# #INDENT# return False #NEWLINE# #UNINDENT# logn = math . log ( n , 2 ) #NEWLINE# max_possible = ( int ( logn ) + 2 ) #NEWLINE# not_square = ( ( n % 10 ) in [ 2 , 3 , 7 , 8 ] ) #NEWLINE# if ( not candidates ) : #NEWLINE# #INDENT# candidates = primerange ( ( 2 + factor ) , max_possible ) #NEWLINE# #UNINDENT# afactor = ( 2 + ( n % 2 ) ) #NEWLINE# for e in candidates : #NEWLINE# #INDENT# if ( e < 3 ) : #NEWLINE# #INDENT# if ( ( e == 1 ) or ( ( e == 2 ) and not_square ) ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# #UNINDENT# if ( e > max_possible ) : #NEWLINE# #INDENT# return False #NEWLINE# #UNINDENT# if factor : #NEWLINE# #INDENT# if ( ( n % afactor ) == 0 ) : #NEWLINE# #INDENT# if ( afactor == 2 ) : #NEWLINE# #INDENT# e = trailing ( n ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# e = multiplicity ( afactor , n ) #NEWLINE# #UNINDENT# if ( e == 1 ) : #NEWLINE# #INDENT# return False #NEWLINE# #UNINDENT# ( r , exact ) = integer_nthroot ( n , e ) #NEWLINE# if ( not exact ) : #NEWLINE# #INDENT# n //= ( afactor ** e ) #NEWLINE# m = perfect_power ( n , candidates = primefactors ( e ) , big = big ) #NEWLINE# if ( m is False ) : #NEWLINE# #INDENT# return False #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# ( r , m ) = m #NEWLINE# g = igcd ( m , e ) #NEWLINE# if ( g == 1 ) : #NEWLINE# #INDENT# return False #NEWLINE# #UNINDENT# m //= g #NEWLINE# e //= g #NEWLINE# ( r , e ) = ( ( ( r ** m ) * ( afactor ** e ) ) , g ) #NEWLINE# #UNINDENT# #UNINDENT# if ( not big ) : #NEWLINE# #INDENT# e0 = primefactors ( e ) #NEWLINE# if ( ( len ( e0 ) > 1 ) or ( e0 [ 0 ] != e ) ) : #NEWLINE# #INDENT# e0 = e0 [ 0 ] #NEWLINE# ( r , e ) = ( ( r ** ( e // e0 ) ) , e0 ) #NEWLINE# #UNINDENT# #UNINDENT# return ( r , e ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# afactor = nextprime ( afactor ) #NEWLINE# #UNINDENT# #UNINDENT# if ( ( logn / e ) < 40 ) : #NEWLINE# #INDENT# b = ( 2.0 ** ( logn / e ) ) #NEWLINE# if ( abs ( ( int ( ( b + 0.5 ) ) - b ) ) > 0.01 ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# #UNINDENT# ( r , exact ) = integer_nthroot ( n , e ) #NEWLINE# if exact : #NEWLINE# #INDENT# if big : #NEWLINE# #INDENT# m = perfect_power ( r , big = big , factor = factor ) #NEWLINE# if ( m is not False ) : #NEWLINE# #INDENT# ( r , e ) = ( m [ 0 ] , ( e * m [ 1 ] ) ) #NEWLINE# #UNINDENT# #UNINDENT# return ( int ( r ) , e ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# return False