Python – Elements with factors count less than K

When working with lists of numbers, you may need to filter elements based on their factor count. This tutorial shows how to find elements that have fewer than K factors using list comprehension and the modulus operator.

Understanding Factors

A factor of a number is any integer that divides the number evenly (with no remainder). For example, the factors of 12 are: 1, 2, 3, 4, 6, and 12.

Method: Using List Comprehension

We'll create a function to count factors and filter elements accordingly ?

def factors(element, K):
    return len([index for index in range(1, element + 1) if element % index == 0]) <= K

my_list = [63, 112, 168, 26, 68]

print("The list is :")
print(my_list)

K = 5
print("The value for K is")
print(K)

my_result = [element for element in my_list if factors(element, K)]

print("The result is :")
print(my_result)
The list is :
[63, 112, 168, 26, 68]
The value for K is
5
The result is :
[26]

How It Works

The factors() function works in these steps:

  • Uses range(1, element + 1) to check all potential factors from 1 to the number itself

  • The condition element % index == 0 identifies actual factors (no remainder when divided)

  • Counts the factors using len() and compares with K

  • Returns True if factor count is less than or equal to K

Step-by-Step Breakdown

Let's examine why 26 is the only result:

def count_factors(n):
    factors = [i for i in range(1, n + 1) if n % i == 0]
    print(f"Factors of {n}: {factors} (count: {len(factors)})")
    return len(factors)

numbers = [63, 112, 168, 26, 68]
K = 5

print(f"Finding numbers with ? {K} factors:\n")

for num in numbers:
    factor_count = count_factors(num)
    if factor_count <= K:
        print(f"? {num} has ? {K} factors")
    else:
        print(f"? {num} has > {K} factors")
    print()
Finding numbers with ? 5 factors:

Factors of 63: [1, 3, 7, 9, 21, 63] (count: 6)
? 63 has > 5 factors

Factors of 112: [1, 2, 4, 7, 8, 14, 16, 28, 56, 112] (count: 10)
? 112 has > 5 factors

Factors of 168: [1, 2, 3, 4, 6, 7, 8, 12, 14, 21, 24, 28, 42, 56, 84, 168] (count: 16)
? 168 has > 5 factors

Factors of 26: [1, 2, 13, 26] (count: 4)
? 26 has ? 5 factors

Factors of 68: [1, 2, 4, 17, 34, 68] (count: 6)
? 68 has > 5 factors

Alternative Implementation

Here's a more readable version with separate factor counting ?

def count_factors(n):
    """Count the number of factors of n"""
    return sum(1 for i in range(1, n + 1) if n % i == 0)

def filter_by_factor_count(numbers, max_factors):
    """Filter numbers with factor count less than or equal to max_factors"""
    return [num for num in numbers if count_factors(num) <= max_factors]

# Test with the same data
data = [63, 112, 168, 26, 68]
K = 5

result = filter_by_factor_count(data, K)
print(f"Numbers with ? {K} factors: {result}")
Numbers with ? 5 factors: [26]

Conclusion

This approach efficiently filters numbers based on their factor count using list comprehension and the modulus operator. Only numbers with factor counts less than or equal to K are included in the result.

Updated on: 2026-03-26T01:02:12+05:30

219 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements