Skip to content
scipython.com

P2.7.2A little number problem

Write a program to find the smallest positive integer, \(n\), whose factorial is not divisible by the sum of its digits. For example, 6 is not such a number because \(6! = 720\) and \(7+2+0=9\) divides 720.

Show solution

The program below reports that 432 is the smallest positive integer whose factorial is not divisble by the sum of its digits.

# Find the smallest positive integer, n, for which n! is *not* divisible
# by the sum of its digits.

def sum_digits(n):
    """ Return the sum of the decimal digits of n. """
    s = 0
    while n:
        s += n % 10
        n //= 10
    return s

n = nfac = 1
while True:
    nfac *= n
    digit_sum = sum_digits(nfac)
    if nfac % digit_sum:
        # Success! report and exit the loop
        print('{}! is not divisible by the sum of its digits'.format(n))
        break
    n += 1