def largest_product(adj):
    """
    Returns the greatest product of adj adjacent digits.
    """
    with open('p008_number.txt') as f:
        numarray = int_to_digit_array(f.read())
    return max(prod(numarray[i:i + adj]) for i in range(len(numarray) - adj))
def maximal_digital_sum(ceil):
    """
    Returns the maximum digital sum of a^b where a, b < ceil.
    """
    max_sum = 0
    for a in range(1, ceil):
        for b in range(1, ceil):
            max_sum = max(max_sum, sum(int_to_digit_array(a ** b)))
    return max_sum
def digit_powers(p):
    """
    Returns the sum of all numbers which can be written as the sum of pth
    powers of their digits.
    """
    total = 0
    sum_digits = 2
    # While the number of digits is still plausible:
    for sum_digits in count(2):
        if sum_digits * 9**p <= 10**(sum_digits - 1):
            break
        for i in range(10**(sum_digits - 1), 10**sum_digits):
            # Remove implausible sums
            if i > sum_digits * 9**p:
                break
            elif i == sum(x**p for x in int_to_digit_array(i)):
                total += i
    return total
Exemple #4
0
def digit_factorials():
    """
    Returns the sum of all numbers which are equal to the sum of the factorial
    of their digits.

    Note that single digit numbers are not included as they are not sums.
    """
    max_digits = 2
    while max_digits * factorial(9) > 10**max_digits:
        max_digits += 1
    sumtotal = 0
    for n in range(2, max_digits + 1):
        for x in combinations_with_replacement(range(10), n):
            if int_array_to_int(x) >= max_digits * factorial(9):
                break
            total = 0
            for digit in x:
                total += factorial(int(digit))
            if sorted(int_to_digit_array(total)) == sorted(x):
                sumtotal += total
    return sumtotal
def factorial_digit_sum(n):
    """
    Returns the sum of the digits in the number n!
    """
    return sum(int_to_digit_array(factorial(n)))
def digit_sum(num):
    """
    Returns the sum of the digits of num.
    """
    return sum(int_to_digit_array(num))