Sum of Prime Factors

Calculate sum of prime factors.

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 and sum prime factors
total = 0
for i in range(2, num + 1):
    if num % i == 0 and is_prime(i):
        total += i

print(f"Sum of prime factors: {total}")

Output

Enter a number: 60
Sum of prime factors: 10

Find prime factors and sum them.

Key Concepts:

  • Find prime factors
  • Sum all prime factors
  • Return total