Prime Factors of Number

Find all prime factors of a number.

Logic BuildingAdvanced
Logic Building
# Helper function
def is_prime(n):
    if n < 2:
        return False
    for i in range(2, int(n ** 0.5) + 1):
        if n % i == 0:
            return False
    return True

# Take number
num = int(input("Enter a number: "))

# Find prime factors
print(f"Prime factors of {num}:")
factors = []
for i in range(2, num + 1):
    if num % i == 0 and is_prime(i):
        factors.append(i)

print(factors)

Output

Enter a number: 60
Prime factors of 60:
[2, 3, 5]

Find factors and check if prime.

Key Concepts:

  • Find all factors
  • Check if factor is prime
  • Collect prime factors