Project Euler Problem #5 - Smallest Multiple (in Python)

# 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
# What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? */

# returns smallest multiple that is evenly divisible by all numbers from 1 - n
# returns -1 if multiple does not exist
def findSmallestMultiple(n):
    for i in range(n, factorial(n) + 1, n):
        if isMultiple(i, n):
            return i
    return -1

# checks every number between 1 and n to see if x is a multiple of every number
# returns True if x is found to be a multiple of every number, and False if x is
# found to not be a multiple of any number
def isMultiple(x, n):
    for i in range(1, n):
        if x % i != 0:
            return False
    return True

# returns the n factorial, or -1 if it does not exist
def factorial(n):
    if n > 1: return n * factorial(n - 1)
    elif n >= 0: return 1
    else: return -1

print (findSmallestMultiple(10)) # 2520
print (findSmallestMultiple(20))
DOWNLOAD

If you're running Python 2.x, the code above will fail for the second call of the function because the for-loop range will "contain too many items".
In that case, use the code below (while-loop instead of for-loop). If you're running Python 3.x, the code above will work just fine.

# 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
# What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? */

# returns smallest multiple that is evenly divisible by all numbers from 1 - n
# returns -1 if multiple does not exist
# works up to n = 20 (long reaches maximum value for values > 20) */
def findSmallestMultiple(n):
    i = n
    while i < factorial(n):
        if isMultiple(i, n):
            return i
        i += n
    return -1

# checks every number between 1 and n to see if x is a multiple of every number
# returns True if x is found to be a multiple of every number, and False if x is
# found to not be a multiple of any number */ 
def isMultiple(x, n):
    for i in range(1, n):
        if x % i != 0:
            return False
    return True

# returns the n factorial, or -1 if it does not exist
def factorial(n):
    if n > 1: return n * factorial(n - 1)
    elif n >= 0: return 1
    else: return -1

print (findSmallestMultiple(10)) # 2520
print (findSmallestMultiple(20))
DOWNLOAD

              Created: February 16, 2014
Completed in full by: Michael Yaworski