# 5 Best Ways to Filter Perfect Squares in a Given Series with Python

Rate this post

π‘ Problem Formulation: Given a sequence of integers, the task is to write a Python program that filters out all the perfect squares. A perfect square is an integer that is the product of an integer with itself. For example, given the list [10, 23, 49, 64, 100], the desired output would be [49, 64, 100], as these are perfect squares.

## Method 1: Using a Loop and Square Root Check

This method involves iterating through each number in the given series, checking if the square root of the number is an integer. If it is, that means the original number was a perfect square, and it gets included in the filtered list. The function `math.sqrt()` can be used to compute the square root, and the `int()` casting can help with checking if the number is an integer.

Here’s an example:

```import math

def filter_perfect_squares(numbers):
perfect_squares = [num for num in numbers if math.sqrt(num).is_integer()]
return perfect_squares

series = [10, 23, 49, 64, 100]
filtered_series = filter_perfect_squares(series)
print(filtered_series)```

Output: [49, 64, 100]

This code snippet defines a function `filter_perfect_squares()` that receives a list of numbers and returns a new list containing only the perfect squares. It utilizes list comprehension to iterate and filter out non-perfect squares by checking if the square root of the number is an integer.

## Method 2: Filtering with a Generator Expression

Generator expressions provide a memory-efficient way to handle large series of numbers. This method is similar to the first but uses a generator inside the square root checking logic to filter perfect squares, thereby saving memory when dealing with large data sets.

Here’s an example:

```import math

def filter_perfect_squares_gen(numbers):
return (num for num in numbers if math.sqrt(num).is_integer())

series = [10, 23, 49, 64, 100]
filtered_series = list(filter_perfect_squares_gen(series))
print(filtered_series)```

Output: [49, 64, 100]

In this snippet, a generator expression within the function `filter_perfect_squares_gen()` yields perfect square numbers from the series one by one, conserving memory. The resulting generator is then cast to a list to realize all of the values at once for output.

## Method 3: Square Sets Precomputation

To boost the efficiency of filtering, especially when dealing with a known range, one can precompute the set of all possible perfect squares in that range. Then the filtering process involves simply checking membership within this precomputed set, which is a constant-time operation.

Here’s an example:

```def generate_square_set(max_root):
return {i**2 for i in range(1, max_root+1)}

def filter_perfect_squares_set(numbers, squares_set):
return [num for num in numbers if num in squares_set]

series = [10, 23, 49, 64, 100]
squares_set = generate_square_set(max(series)**0.5)
filtered_series = filter_perfect_squares_set(series, squares_set)
print(filtered_series)```

Output: [49, 64, 100]

In this code, `generate_square_set()` function creates a set of perfect squares up to the square root of the maximum value in the series. `filter_perfect_squares_set()` then uses this precomputed set to quickly filter out the perfect squares from the list.

## Method 4: Using the Built-in `filter()` Function

The Python built-in `filter()` function can be used to filter perfect squares by supplying a filter function that implements the square root integer check. This method is more functional and can be useful when keeping the code concise is necessary.

Here’s an example:

```import math

def is_perfect_square(num):
return math.sqrt(num).is_integer()

series = [10, 23, 49, 64, 100]
filtered_series = filter(is_perfect_square, series)
print(list(filtered_series))```

Output: [49, 64, 100]

The functional approach with `filter()` separates the logic for checking a perfect square into its own function `is_perfect_square()`. This increases modularity and testability of the code, producing a filtered list when combined with the original series.

## Bonus One-Liner Method 5: The Power of Lambda Functions

Lambda functions in Python allow one to succinctly express the square root check within the list comprehension itself. This method reduces the code length significantly with a tradeoff of potentially reducing readability.

Here’s an example:

```import math

series = [10, 23, 49, 64, 100]
filtered_series = list(filter(lambda num: math.sqrt(num).is_integer(), series))
print(filtered_series)```

Output: [49, 64, 100]

This compact one-liner uses a lambda function within the `filter()` call to perform the square root check. It is an inline, anonymous function that provides a quick, functional way to filter out the list.

## Summary/Discussion

• Method 1: Loop and Square Root Check. Simple to understand. Not as efficient for large datasets.
• Method 2: Generator Expression. Memory-efficient for large datasets. May be slightly trickier to understand for beginners.
• Method 3: Square Sets Precomputation. Very fast for large datasets with known range. Requires additional space for storing the precomputed set and is less flexible if the range is not known in advance.
• Method 4: Use of `filter()` Function. Functional programming style. Separation of concerns makes it easily testable but might be less intuitive to those not familiar with functional approaches.
• Method 5: Lambda Functions. Concise one-liner. Efficient, but can sacrifice readability for brevity.