Count Composite Numbers

Count composite numbers in range.

Logic BuildingIntermediate
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 range
a = int(input("Enter start: "))
b = int(input("Enter end: "))

# Count composites
count = 0
for num in range(a, b + 1):
    if num > 1 and not is_prime(num):
        count += 1

print(f"Composite numbers: {count}")

Output

Enter start: 1
Enter end: 20
Composite numbers: 11

Count numbers that are not prime.

Key Concepts:

  • Composite = not prime and > 1
  • Check each number
  • Count composites