Open In App

Python – Interlist Perfect Square Pairs

Last Updated : 05 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working in Mathematics domain, we can have a problem in which we need to check if product of elements from different lists can lead to perfect square or not. This can have application in many domains including mathematics and web development. Lets discuss certain ways in which this task can be performed. 

Method #1 : Using loop 

This task can be performed using loop. This is brute force way in which we can perform this task. In this, we multiple each element with other and check if it is perfect square. 

Python3




# Python3 code to demonstrate
# Interlist Perfect Square Pairs
# using loop
 
# Initializing lists
test_list1 = [4, 5, 6, 7, 3, 4]
test_list2 = [6, 4, 2, 8, 9, 4]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# Flatten List to individual elements
# using chain() + isinstance()
res = []
idx = 0
while(idx < len(test_list1)):
    j = 0
    while(j < len(test_list2)):
        sub = test_list1[idx] * test_list2[j]
        n = sub**0.5
        temp = int(n)
        if n == temp:
            res.append((test_list1[idx], test_list2[j]))
        j = j + 1
    idx = idx + 1
             
# printing result
print ("The perfect square pairs are : " + str(res))


Output : 

The original list 1 is : [4, 5, 6, 7, 3, 4] The original list 2 is : [6, 4, 2, 8, 9, 4] The perfect square pairs are : [(4, 4), (4, 9), (4, 4), (6, 6), (4, 4), (4, 9), (4, 4)]

Time Complexity: O(n*n) where n is the number of elements in the list “test_list”. 
Auxiliary Space: O(n) where n is the number of elements in the list “test_list”.

Method #2: Using Counter() + set() + loop + product() 

This task can also be performed using combination of these functions. In this we use the fact that the perfect square with always factors in pairs that is each element factor would be even. 

Python3




# Python3 code to demonstrate
# Interlist Perfect Square Pairs
# using Counter() + set() + loop + product()
from itertools import product
from collections import Counter
 
 
def prime_factors(n):
    i = 2
    factors = []
    while i * i & lt
    = n:
        if n % i:
            i += 1
        else:
            n //= i
            factors.append(i)
    if n & gt
    1:
        factors.append(n)
    return Counter(factors)
 
 
# Initializing lists
test_list1 = [4, 5, 6, 7, 3, 4]
test_list2 = [6, 4, 2, 8, 9, 4]
 
# printing original lists
print(& quot
       The original list 1 is : & quot
       + str(test_list1))
print(& quot
       The original list 2 is : & quot
       + str(test_list2))
 
# Flatten List to individual elements
# using Counter() + set() + loop + product()
prime_fac = {idx: prime_factors(idx)
             for idx in set(test_list1) | set(test_list2)}
res = set()
for a, b in product(test_list1, test_list2):
    combined_counts = prime_fac[a] + prime_fac[b]
    if all(v % 2 == 0 for v in combined_counts.values()):
        res.add(tuple(sorted([a, b])))
 
# printing result
print (& quot
        The perfect square pairs are : & quot
        + str(res))


Output : 

The original list 1 is : [4, 5, 6, 7, 3, 4] The original list 2 is : [6, 4, 2, 8, 9, 4] The perfect square pairs are : {(4, 4), (4, 9), (6, 6)}

Time Complexity: O(n*n),The above code iterates through the list once, hence the time complexity is quadratic, i.e. O(n*n).
Auxiliary Space: O(n),The algorithm uses an additional list to store the result, thus consuming linear space which is O(n).

Method 3: Using nested list comprehensions

  1. Initialize two lists test_list1 and test_list2 with some integer values.
  2. Print the original lists using the print() function with the concatenation of strings and the conversion of the lists to strings using the str() function.
  3. Use nested list comprehension to find all possible pairs of elements from test_list1 and test_list2 and store them in the pairs list.
  4. Iterate over the pairs list using another list comprehension to filter out only those pairs that are perfect squares and store them in the perfect_square_pairs list.
  5. Print the perfect_square_pairs list using the print() function.

Note: The program finds all the pairs of integers from test_list1 and test_list2 and filters out only the perfect square pairs. The final output is the list of all perfect square pairs.

Python3




# Initializing lists
test_list1 = [4, 5, 6, 7, 3, 4]
test_list2 = [6, 4, 2, 8, 9, 4]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# Finding perfect square pairs using nested list comprehensions
pairs = [(x, y) for x in test_list1 for y in test_list2]
perfect_square_pairs = [(x, y) for x, y in pairs if int((x * y)**0.5)**2 == x * y]
 
# printing result
print("The perfect square pairs are:", perfect_square_pairs)


Output

The original list 1 is : [4, 5, 6, 7, 3, 4]
The original list 2 is : [6, 4, 2, 8, 9, 4]
The perfect square pairs are: [(4, 4), (4, 9), (4, 4), (6, 6), (4, 4), (4, 9), (4, 4)]

Time complexity: O(n^2) because it involves iterating over all pairs of elements from the two lists.
Auxiliary Space: O(n^2) because it generates a list of all pairs of elements.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads