def isMorphic( n, k ): ''' Returns true if n to the k power ends with n. ''' digits = getMPFIntegerAsString( real_int( n ) ) sqr_digits = getMPFIntegerAsString( power( n, real_int( k ) ) ) start = len( sqr_digits ) - len( digits ) return 1 if ( sqr_digits[ start : ] == digits ) else 0
def isPandigital( n ): str = getMPFIntegerAsString( n ) for c in string.digits: if c not in str: return 0 return 1
def getRightTruncations( n ): if n < 0: raise ValueError( '\'get_right_truncations\' requires a positive argument' ) str = getMPFIntegerAsString( n ) for i in range( len( str ), 0, -1 ): yield mpmathify( str[ 0 : i ] )
def getLeftTruncations( n ): if n < 0: raise ValueError( '\'get_left_truncations\' requires a positive argument' ) str = getMPFIntegerAsString( n ) for i, e in enumerate( str ): yield mpmathify( str[ i : ] )
def sumDigits( n ): str = getMPFIntegerAsString( n ) result = 0 for c in str: result = fadd( result, int( c ) ) return result
def isPalindrome( n ): str = getMPFIntegerAsString( n ) length = len( str ) for i in range( 0, length // 2 ): if str[ i ] != str[ -( i + 1 ) ]: return 0 return 1
def getDigits( n, dropZeroes = False ): str = getMPFIntegerAsString( n ) result = [ ] for c in str: if dropZeroes and c == '0': continue result.append( int( c ) ) return result
def isPolydivisible( n ): if real_int( n ) < 0: raise ValueError( 'non-negative, real integer expected' ) strValue = getMPFIntegerAsString( n ) # a couple of cheats if ( len( strValue ) > 4 ) and ( strValue[ 4 ] not in [ '5', '0' ] ): return 0 if ( len( strValue ) > 9 ) and ( strValue[ 9 ] != '0' ): return 0 for i in range( len( strValue ), 1, -1 ): current = mpmathify( strValue[ : i ] ) if not isDivisible( current, i ): return 0 return 1
def getRightTruncations( n ): str = getMPFIntegerAsString( n ) for i in range( len( str ), 0, -1 ): yield mpmathify( str[ 0 : i ] )
def getLeftTruncations( n ): str = getMPFIntegerAsString( n ) for i in range( len( str ) ): yield mpmathify( str[ i : ] )
def splitNumberByDigits( n ): str = getMPFIntegerAsString( n ) split = len( str ) // 2 return mpmathify( str[ : split ] ), mpmathify( str[ split : ] )
def reverseDigits( n ): return mpmathify( getMPFIntegerAsString( n )[ : : -1 ] )