find number that is divisible through range of numbers in python

Here is one way to find a number that is divisible through a range of numbers in Python:

main.py
import math

# Define a function to check if a number is divisible through a range of numbers
def is_divisible(num, start, end):
    for i in range(start, end+1):
        if num % i != 0:
            return False
    return True

# Find the smallest number that is divisible through a range of numbers
def smallest_divisible(start, end):
    num = end
    while True:
        if is_divisible(num, start, end):
            return num
        num += end   # Increment the number by the highest multiple in the range

# Example usage:
print(smallest_divisible(1, 10))   # Output: 2520
582 chars
20 lines

In this code, we define a function is_divisible that takes three arguments: a number num, and the starting and ending values of the range. This function checks if num is divisible through all the numbers in the range by iterating over the range with a for loop and returning False as soon as a number in the range does not divide num evenly. If the loop completes without returning False, the function returns True.

We then define a second function smallest_divisible that takes the starting and ending values of the range as arguments. This function initializes a starting number num equal to the ending number of the range, and then uses a while loop to increment num by the highest multiple in the range until it finds the smallest number that is divisible through the entire range. This is done by calling the is_divisible function with num, start, and end as arguments until is_divisible returns True, at which point smallest_divisible returns the value of num.

Finally, we demonstrate the use of the smallest_divisible function by calling it with a range of (1, 10) and printing the result, which should be the smallest number that is divisible through all the integers between 1 and 10, inclusive.

gistlibby LogSnag