def is_pandigital(n): i = 1 digits_count = [0] * 10 while digits_count.count(1) != 9: for digit in digits(n * i): if digit == 0 or digits_count[digit] > 1: return (False, 0) else: digits_count[digit] += 1 i += 1 num = "".join("".join(str(d) for d in digits(n * j)[::-1]) for j in range(1, i)) return (True, num)
def stop89(n): if n == 1: return False if n == 89: return True if n in memo: return memo[n] result = stop89(sumsq(digits(n))) memo[n] = result return result
def isPowerSum_s(s, x): _, d = digits(s ** x) if sum(d) == s: print(s, x, s**x) return True else: return False
def problem16(): """ 215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. What is the sum of the digits of the number 2^1000? """ return sum(util.digits(2**1000))
def keep_10_multiply(a, b): r = a * b num, ds = digits(r) if num > 10: ds = ds[-10:] keep_10 = int(''.join(map(str, ds))) return keep_10
def is_pandigital_product(a, b): """Determine whether a * b is a pandigital product.""" missing = [True]*9 pandigits = [1, 2, 3, 4, 5, 6, 7, 8, 9] if a*b < 10**8: total_digits = digits(a) + digits(b) + digits(a*b) if len(total_digits) != 9: return False for d in total_digits: if d == 0: return False missing[d-1] = False return not any(missing)
def chainEnds(n): # chains = [] while n != 1 and n != 89: _, allDigits = digits(n); n = sum([x**2 for x in allDigits]) # chains.append(n) # return chains return n
def bouncy(x): digs = digits(x) up, down = False, False for i in range(1,len(digs)): if digs[i-1] < digs[i]: down = True elif digs[i-1] > digs[i]: up = True if up and down: return True return False
def is_pandigital(n): [num, ds] = digits(n) p = [True] * num for d in ds: if d == 0 or d > num: break else: p[d-1] = False # print n, p if len(filter(lambda x: x, p)) == 0: return True else: return False
def isBouncyNumber(n): numDigits, digitList = digits(n) if numDigits < 2: return False index = 0 trend = 0 while trend == 0 and index < numDigits-1: trend = digitList[index] - digitList[index+1] index += 1 # print(trend, index) for i in range(index, numDigits-1): t = digitList[i] - digitList[i+1] if t * trend < 0: return True return False
def is_truncatable_prime(n): num, ds = digits(n) # remove digits from left to right for i in xrange(1, len(ds)): tmp_ds = ds[i:] tmp_n = int(reduce(lambda x, y: ''.join([str(x), str(y)]), tmp_ds, '')) if not is_prime(tmp_n): return False # remove digits from right to left for i in xrange(1, len(ds)): tmp_ds = ds[:len(ds)-i] tmp_n = int(reduce(lambda x, y: ''.join([str(x), str(y)]), tmp_ds, '')) if not is_prime(tmp_n): return False return True
def problem30(): """ Find the sum of all the numbers that can be written as the sum of fifth powers of their digits. """ powers = [0, 1, 32, 243, 1024, 3125, 7776, 16807, 32768, 59049] total, i = 0, 3 while i < 1000000: num_total = 0 for d in util.digits(i): num_total += powers[d] if num_total == i: total += i i += 1 return total
def problem56(): """ A googol (10100) is a massive number: one followed by one-hundred zeros; 100100 is almost unimaginably large: one followed by two-hundred zeros. Despite their size, the sum of the digits in each number is only 1. Considering natural numbers of the form, a^b, where a, b < 100, what is the maximum digital sum? """ r = range(1, 101) m = 0 for a in r: for b in r: m = max(m, sum(util.digits(pow(a, b)))) return m
def problem52(): """ It can be seen that the number, 125874, and its double, 251748, contain exactly the same digits, but in a different order. Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits. """ i = 1 while True: products = [i*j for j in xrange(1, 7)] testdigits = [sorted(util.digits(p)) for p in products] for test in testdigits: if test != testdigits[0]: break else: return i i += 1
def problem34(): """ 145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145. Find the sum of all numbers which are equal to the sum of the factorial of their digits. Note: as 1! = 1 and 2! = 2 are not sums they are not included. """ facs = [1, 1, 2, 6, 24, 120, 720, 504, 40320, 362880] total, i = 0, 3 while i < 1000000: num_total = 0 for d in util.digits(i): num_total += facs[d] if num_total == i: total += i i += 1 return total
def is_pandigital(a): _, d = digits(a) return (0 not in d) and (len(set(a)) == 9)
def fact_digits(x): return sum(factorial(i) for i in digits(x))
from util import digits from continued_fraction import convergent answer = 0 con = convergent([1], [1, 2]) top = 1000 # ignoring the first convergent in this problem con.next() for i in range(top): n, d = con.next() if len(digits(n)) > len(digits(d)): answer += 1 print answer
def problem20(): """ Find the sum of the digits in the number 100! """ n = reduce(lambda x,y: x*y, xrange(1, 101)) return sum(util.digits(n))
def permutable(x, y): [num, dx] = digits(x) [num, dy] = digits(y) strx = reduce(lambda x, y: ''.join([str(x), str(y)]), sorted(dx), '') stry = reduce(lambda x, y: ''.join([str(x), str(y)]), sorted(dy), '') return strx == stry
from util import digits from continued_fraction import convergent answer = 0 con = convergent([1], [1,2]) top = 1000 #ignoring the first convergent in this problem con.next() for i in range(top): n, d = con.next() if len(digits(n)) > len(digits(d)): answer += 1 print answer
from continued_fraction import convergent from util import digits def seq(): yield 2 k = 1 while True: yield 1 yield 2 * k yield 1 k += 1 con = convergent([1], seq()) top = 100 for i in range(top): n,d = con.next() print sum(digits(n))
def fifth_digits(n): """Return the sum of the digits of n raised to the fifth power""" return sum(map(lambda x: x**5, digits(n)))
from continued_fraction import convergent from util import digits def seq(): yield 2 k = 1 while True: yield 1 yield 2 * k yield 1 k += 1 con = convergent([1], seq()) top = 100 for i in range(top): n, d = con.next() print sum(digits(n))
def solution(): return sum([n for n in range(3, 50000) if (sum(map(fact, digits(n))) == n)])
from util import digits answer = 0 for a in range(100): for b in range(100): answer = max(answer, sum(digits(a**b))) print answer
def main(): print sum(i for i in range(3,50000) if sum(factorial(d) for d in digits(i)) == i)
def is_palindrome(n): """Determine whether n is a palindrome.""" d = digits(n) return d == d[::-1]
def more_digits(x,y): return len(digits(x)) > len(digits(y))
def solution(): return sum( [n for n in range(3, 50000) if (sum(map(fact, digits(n))) == n)])
def power_of_digits(n,p): return sum(i**p for i in digits(n))