Python Program to Find Numbers Divisible by 7 and Multiple of 5 in a Given Range

Finding numbers divisible by 7 and multiples of 5 within a range is a common programming problem. Python provides several approaches: using the modulo operator, iterative addition, and multiplication methods. Each approach has different performance characteristics and use cases.

Method 1: Using Modulo Operator

The most straightforward approach uses the modulo operator (%) to check divisibility. A number is divisible by 7 if number % 7 == 0, and a multiple of 5 if number % 5 == 0.

Algorithm

Step 1 ? Define the range with lower and upper bounds.

Step 2 ? Iterate through the range and check if each number is divisible by 7.

Step 3 ? Iterate through the range and check if each number is a multiple of 5.

Step 4 ? Find numbers that satisfy both conditions (divisible by both 7 and 5).

low_num = 50
high_num = 100

print(f"Range: {low_num} to {high_num}")

# Numbers divisible by 7
divisible_by_7 = []
for num in range(low_num, high_num + 1):
    if num % 7 == 0:
        divisible_by_7.append(num)

print(f"\nNumbers divisible by 7: {divisible_by_7}")

# Numbers multiple of 5
multiple_of_5 = []
for num in range(low_num, high_num + 1):
    if num % 5 == 0:
        multiple_of_5.append(num)

print(f"Numbers multiple of 5: {multiple_of_5}")

# Numbers divisible by both 7 and 5
both_conditions = []
for num in range(low_num, high_num + 1):
    if num % 7 == 0 and num % 5 == 0:
        both_conditions.append(num)

print(f"Numbers divisible by both 7 and 5: {both_conditions}")
Range: 50 to 100

Numbers divisible by 7: [56, 63, 70, 77, 84, 91, 98]
Numbers multiple of 5: [50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100]
Numbers divisible by both 7 and 5: [70]

Method 2: Using Step-wise Addition

This method finds the first valid number in the range and then adds the divisor repeatedly to generate subsequent multiples within the range.

low_num = 200
high_num = 300

# Find multiples of 7 using addition
multiples_of_7 = []
# Find first multiple of 7 in range
first_multiple_7 = low_num + (7 - low_num % 7) % 7
current = first_multiple_7

while current <= high_num:
    multiples_of_7.append(current)
    current += 7

# Find multiples of 5 using addition
multiples_of_5 = []
first_multiple_5 = low_num + (5 - low_num % 5) % 5
current = first_multiple_5

while current <= high_num:
    multiples_of_5.append(current)
    current += 5

print(f"Range: {low_num} to {high_num}")
print(f"Multiples of 7: {multiples_of_7}")
print(f"Multiples of 5: {multiples_of_5}")

# Find common numbers
common_multiples = [num for num in multiples_of_7 if num in multiples_of_5]
print(f"Numbers divisible by both 7 and 5: {common_multiples}")
Range: 200 to 300
Multiples of 7: [203, 210, 217, 224, 231, 238, 245, 252, 259, 266, 273, 280, 287, 294]
Multiples of 5: [200, 205, 210, 215, 220, 225, 230, 235, 240, 245, 250, 255, 260, 265, 270, 275, 280, 285, 290, 295, 300]
Numbers divisible by both 7 and 5: [210, 245, 280]

Method 3: Using Multiplication Method

This approach calculates the multiplier values directly and generates multiples by multiplication rather than repeated addition.

low_num = 200
high_num = 300

# Find multiples of 7 using multiplication
multiples_of_7 = []
start_multiplier_7 = (low_num + 6) // 7  # Ceiling division
multiplier = start_multiplier_7

while True:
    multiple = 7 * multiplier
    if multiple > high_num:
        break
    if multiple >= low_num:
        multiples_of_7.append(multiple)
    multiplier += 1

# Find multiples of 5 using multiplication
multiples_of_5 = []
start_multiplier_5 = (low_num + 4) // 5  # Ceiling division
multiplier = start_multiplier_5

while True:
    multiple = 5 * multiplier
    if multiple > high_num:
        break
    if multiple >= low_num:
        multiples_of_5.append(multiple)
    multiplier += 1

print(f"Range: {low_num} to {high_num}")
print(f"Multiples of 7: {multiples_of_7}")
print(f"Multiples of 5: {multiples_of_5}")

# Find intersection
common_multiples = list(set(multiples_of_7) & set(multiples_of_5))
common_multiples.sort()
print(f"Numbers divisible by both 7 and 5: {common_multiples}")
Range: 200 to 300
Multiples of 7: [203, 210, 217, 224, 231, 238, 245, 252, 259, 266, 273, 280, 287, 294]
Multiples of 5: [200, 205, 210, 215, 220, 225, 230, 235, 240, 245, 250, 255, 260, 265, 270, 275, 280, 285, 290, 295, 300]
Numbers divisible by both 7 and 5: [210, 245, 280]

Optimized Solution Using LCM

Numbers divisible by both 7 and 5 are multiples of their LCM (Least Common Multiple). Since 7 and 5 are coprime, LCM(7,5) = 35.

import math

def find_multiples_optimized(low, high):
    """Find numbers divisible by 7, multiples of 5, and both using LCM"""
    
    # Multiples of 7
    multiples_7 = list(range(
        low + (7 - low % 7) % 7,  # First multiple of 7 in range
        high + 1,
        7
    ))
    
    # Multiples of 5  
    multiples_5 = list(range(
        low + (5 - low % 5) % 5,  # First multiple of 5 in range
        high + 1,
        5
    ))
    
    # Multiples of both (LCM of 7 and 5 = 35)
    lcm = (7 * 5) // math.gcd(7, 5)  # LCM = 35
    multiples_both = list(range(
        low + (lcm - low % lcm) % lcm,
        high + 1,
        lcm
    ))
    
    return multiples_7, multiples_5, multiples_both

# Test with range 100-200
low, high = 100, 200
div_7, mult_5, both = find_multiples_optimized(low, high)

print(f"Range: {low} to {high}")
print(f"Divisible by 7: {div_7}")
print(f"Multiple of 5: {mult_5}")
print(f"Divisible by both: {both}")
Range: 100 to 200
Divisible by 7: [105, 112, 119, 126, 133, 140, 147, 154, 161, 168, 175, 182, 189, 196]
Multiple of 5: [100, 105, 110, 115, 120, 125, 130, 135, 140, 145, 150, 155, 160, 165, 170, 175, 180, 185, 190, 195, 200]
Divisible by both: [105, 140, 175]

Performance Comparison

Method Time Complexity Space Complexity Best For
Modulo Check O(n) O(1) Small ranges, learning
Step Addition O(n/k) O(1) Large ranges
Multiplication O(n/k) O(1) Mathematical clarity
LCM Method O(n/k) O(1) Multiple conditions

Conclusion

The modulo method is simplest for learning, while step addition and multiplication methods are more efficient for large ranges. Use the LCM approach when finding numbers satisfying multiple divisibility conditions simultaneously.

Updated on: 2026-03-27T07:18:26+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements