You are given two lists and you have to find out the pairs which make a perfect square.
For example in:
a = [2, 6, 10, 13, 17, 18]
b = [3, 7, 8, 9, 11, 15]
There are two pairs (2,8)
and (8,18)
.
Is there any method efficient than the brute-force? Here is my code which has a time complexity of O(n*m) (where n is the length of a and m is the length of b).
pl = []
a = [ 2, 6, 10, 13, 17,18]
b = [ 3, 7, 8, 9, 11, 15 ]
i = 0
while(i < len(a)):
j = 0
while(j < len(b)):
p = a[i]*b[j]
n = p**0.5
u = int(n)
if n == u:
pl.append((a[i],b[j]))
j = j+1
i = i+1
print(pl)
This question has been asked before using C# here, but I don't get what they mean by "All we would need to store for each number is which of its prime factors have an odd count", so I can't implement this in my Python code.
Can someone explain to me how we might implement an efficient solution in Python?