Find the number of consecutive zero at the end after multiplying n numbers in Python

When multiplying numbers, trailing zeros are formed by factors of 10, which come from pairs of factors 2 and 5. To find consecutive zeros at the end of a product, we count the factors of 2 and 5 in all numbers and take the minimum count.

So, if the input is like [200, 20, 5, 30, 40, 14], then the output will be 6 as 200 × 20 × 5 × 30 × 40 × 14 = 336000000, there are six 0s at the end.

Algorithm Approach

To solve this problem, we follow these steps:

  • Count factors of 2 in each number

  • Count factors of 5 in each number

  • Sum up all factors of 2 and factors of 5 separately

  • Return the minimum of these two sums (since each trailing zero needs one factor of 2 and one factor of 5)

Helper Functions

First, we define helper functions to count factors of 2 and 5:

def count_fact_two(n):
    """Count the number of times 2 divides n"""
    count = 0
    while n % 2 == 0:
        count += 1
        n = n // 2
    return count

def count_fact_five(n):
    """Count the number of times 5 divides n"""
    count = 0
    while n % 5 == 0:
        count += 1
        n = n // 5
    return count

# Test the helper functions
print("Factors of 2 in 200:", count_fact_two(200))
print("Factors of 5 in 200:", count_fact_five(200))
Factors of 2 in 200: 3
Factors of 5 in 200: 2

Complete Solution

Now we implement the main function that counts consecutive zeros:

def count_fact_two(n):
    count = 0
    while n % 2 == 0:
        count += 1
        n = n // 2
    return count

def count_fact_five(n):
    count = 0
    while n % 5 == 0:
        count += 1
        n = n // 5
    return count

def get_consecutive_zeros(numbers):
    twos = 0
    fives = 0
    
    for num in numbers:
        twos += count_fact_two(num)
        fives += count_fact_five(num)
    
    # Return minimum because each trailing zero needs one 2 and one 5
    return min(twos, fives)

# Example usage
numbers = [200, 20, 5, 30, 40, 14]
result = get_consecutive_zeros(numbers)
print(f"Numbers: {numbers}")
print(f"Consecutive zeros: {result}")

# Verify by actual multiplication
product = 1
for num in numbers:
    product *= num
print(f"Actual product: {product}")
Numbers: [200, 20, 5, 30, 40, 14]
Consecutive zeros: 6
Actual product: 336000000

How It Works

Let's trace through the example [200, 20, 5, 30, 40, 14]:

Number Factors of 2 Factors of 5
200 = 2³ × 5² 3 2
20 = 2² × 5 2 1
5 = 5 0 1
30 = 2 × 3 × 5 1 1
40 = 2³ × 5 3 1
14 = 2 × 7 1 0
Total 10 6

Since we have 10 factors of 2 and 6 factors of 5, we can form min(10, 6) = 6 trailing zeros.

Conclusion

To count consecutive zeros in a product, count factors of 2 and 5 in all numbers and take the minimum. This approach is efficient as it avoids computing the large product directly and works by understanding that trailing zeros come from pairs of factors 2 and 5.

---
Updated on: 2026-03-25T09:51:32+05:30

303 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements