human
Give me a code to count pairs whose product contains single distinct prime factor and the other steps required given the following:
Count pairs whose product contains single distinct prime factor | Function to find a single distinct prime factor of N.
Stores distinct prime factors of N.
Calculate prime factor of N.
Calculate distinct prime factor.
Insert i into disPrimeFact.
Update N.
If N is not equal to 1.
Insert N into disPrimeFact.
If N contains a single distinct prime factor.
Return single distinct prime factor of N.
If N contains more than one distinct prime factor.
Function to count pairs in the array whose product contains only single distinct prime factor.
Stores count of 1 s in the array.
mp [ i ] : Stores count of array elements whose distinct prime factor is only i.
Traverse the array arr [ ].
If current element is 1.
Store distinct prime factor of arr [ i ].
If arr [ i ] contains more than one prime factor.
If arr [ i ] contains a single prime factor.
Stores the count of pairs whose product of elements contains only a single distinct prime factor.
Traverse the map mp [ ].
Stores count of array elements whose prime factor is ( it . first ).
Update res.
Driver Code"
Solution in Python:
assistant
#Here is code to count pairs whose product contains single distinct prime factor and the other steps required.
 def singlePrimeFactor ( N ) :
	disPrimeFact = { }
	for i in range ( 2 , N + 1 ) :
		if i * i > N :
			break
		while ( N % i == 0 ) :
			disPrimeFact [ i ] = 1
			N //= i
	if ( N != 1 ) :
		disPrimeFact [ N ] = 1
	if ( len ( disPrimeFact ) == 1 ) :
		return list ( disPrimeFact . keys ( ) ) [ 0 ]
	return - 1
def cntsingleFactorPair ( arr , N ) :
	countOf1 = 0
	mp = { }
	for i in range ( N ) :
		if ( arr [ i ] == 1 ) :
			countOf1 += 1
			continue
		factorValue = singlePrimeFactor ( arr [ i ] )
		if ( factorValue == - 1 ) :
			continue
		else :
			mp [ factorValue ] = mp . get ( factorValue , 0 ) + 1
	res = 0
	for it in mp :
		X = mp [ it ]
		res += countOf1 * X + ( X * ( X - 1 ) ) // 2
	return res
if __name__ == ' _ _ main _ _ ' :
	arr = [ 1 , 2 , 3 , 4 ]
	N = len ( arr )
	print ( cntsingleFactorPair ( arr , N ) )