Count pairs in array such that one element is power of another | Python3 program to count pairs in array such that one element is power of another ; Function to check if given number number y is power of x ; log function to calculate value ; compare to the result1 or result2 both are equal ; Function to find pairs from array ; Iterate through all pairs ; Increment count if one is the power of other ; Driver code
from math import log NEW_LINE def isPower ( x , y ) : NEW_LINE INDENT res1 = log ( y ) // log ( x ) ; NEW_LINE res2 = log ( y ) / log ( x ) ; NEW_LINE return ( res1 == res2 ) ; NEW_LINE DEDENT def countPower ( arr , n ) : NEW_LINE INDENT res = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if isPower ( arr [ i ] , arr [ j ] ) or isPower ( arr [ j ] , arr [ i ] ) : NEW_LINE INDENT res += 1 ; NEW_LINE DEDENT DEDENT DEDENT return res ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 16 , 2 , 3 , 9 ] ; NEW_LINE n = len ( a ) ; NEW_LINE print ( countPower ( a , n ) ) ; NEW_LINE DEDENT