How to find the smallest number that is evenly divisible by all of the numbers from 1 to 10 in Python

2 Answers

0 votes
import math  

# Function to compute the Least Common Multiple (LCM) of two numbers
def lcm(a, b):
    return abs(a * b) // math.gcd(a, b)  # Use the absolute value and integer division to find LCM

# Function to compute the LCM of multiple numbers in a list
def lcm_multiple(numbers):
    result = numbers[0]  # Start with the first number in the list
    for number in numbers[1:]:  # Iterate through the remaining numbers
        result = lcm(result, number)  # Compute the LCM of the current result and the next number
    return result  # Return the final LCM of all numbers

# Define a range of numbers from 1 to 10 (inclusive)
numbers = range(1, 11)

# Compute the smallest multiple that is evenly divisible by all numbers from 1 to 10
smallest_multiple = lcm_multiple(numbers)

print(smallest_multiple)



'''
run:

The largest palindrome made from the product of two 2-digit numbers is: 2520

'''

 



answered May 11 by avibootz
0 votes
# Function to find the Least Common Multiple (LCM) of two numbers
def lcm(a, b):
    # Start with the greater of the two numbers
    lmc = max(a, b)

    # Loop indefinitely until we find the LCM
    while True:
        # If `lmc` is divisible by both numbers, it is the LCM
        if lmc % a == 0 and lmc % b == 0:
            return lmc  # Return the LCM
        lmc += 1  # Increment and check the next number

# Variable to store the LCM of numbers from 1 to 10
result = 1

# Iterate through numbers 1 to 10
for i in range(1, 11):
    result = lcm(result, i)  # Update `result` with LCM of current range

# Output the final LCM of numbers from 1 to 10
print("The largest palindrome made from the product of two 2-digit numbers is:", result)




'''
run:

The largest palindrome made from the product of two 2-digit numbers is: 2520

'''

 



answered May 11 by avibootz
...