def is_stupid(n, d): ndigits = get_digits(n) ddigits = get_digits(d) if ndigits[1] == ddigits[0]: if ddigits[1] != 0: if Fraction(ndigits[0], ddigits[1]) == Fraction(n, d): return True return False
def rotations(n): "Return the set of rotations of the given n. e.g. 37 outputs {37, 73}." digits = get_digits(n) result = set() for i in range(0, len(digits)): result.add(combine_digits(digits[i:] + digits[0:i])) return result
def find_curious_numbers(): # Precompute factorials facts = [factorial(x) for x in range(0, 10)] # Numbers obeying this rule cannot be larger than 7 digits, because # 9! * 8 is 2903040, a 7-digit number. (An 8 digit number can never # produce itself in this way.) limit = facts[9] * 7 + 1 is_curious = lambda n: sum([facts[x] for x in get_digits(n)]) == n return filter(is_curious, xrange(10, limit))
def get_permutations(x): "Return a list of all permutations of digits of x (as integers)." digits = get_digits(x) if len(digits) == 1: return digits result = [] for i in range(0, len(digits)): combine = lambda d, e: int("".join(map(str, d)) + "".join(map(str, e))) result += map(lambda x: x*10 + digits[i], get_permutations(combine(digits[:i], digits[i+1:]))) return list(set(result)) # Hacky duplicate filter
def get_permutations(x): "Return a list of all permutations of digits of x (as integers)." digits = get_digits(x) if len(digits) == 1: return digits result = [] for i in range(0, len(digits)): combine = lambda d, e: int("".join(map(str, d)) + "".join(map(str, e))) result += map(lambda x: x * 10 + digits[i], get_permutations(combine(digits[:i], digits[i + 1:]))) return list(set(result)) # Hacky duplicate filter
def is_palindrome(number): """ Return true if the number is a palindrome. Start dividing the number by 10, 100, 1000, etc to get the digits from the right-hand side of the number. """ digits = get_digits(number) palindrome = True for (k, v) in zip(digits, reversed(digits)): if k != v: palindrome = False break return palindrome
def same_digits(numbers): expect = sorted(get_digits(numbers[0])) for i in range(1, len(numbers)): if sorted(get_digits(numbers[i])) != expect: return False return True
def is_narcissistic(n, exp): value = 0 digits = get_digits(n) for digit in digits: value += digit**exp return value == n
#!/usr/bin/env python """ A googol (10^100) is a massive number: one followed by one-hundred zeros; 100^100 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? """ from digits import get_digits if __name__ == "__main__": maxsum = 0 for a in range(0, 100): for b in range(0, 100): s = sum(get_digits(a ** b)) if s > maxsum: maxsum = s maxa = a maxb = b print "%d^%d = %d" % (maxa, maxb, maxa ** maxb) print "Sum: %d" % maxsum
#!/usr/bin/env python """ A googol (10^100) is a massive number: one followed by one-hundred zeros; 100^100 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? """ from digits import get_digits if __name__ == '__main__': maxsum = 0 for a in range(0, 100): for b in range(0, 100): s = sum(get_digits(a**b)) if s > maxsum: maxsum = s maxa = a maxb = b print "%d^%d = %d" % (maxa, maxb, maxa**maxb) print "Sum: %d" % maxsum
#!/usr/bin/env python # -*- coding: utf-8 -*- import sys sys.path.append('../lib') from digits import get_digits print sum(reversed(get_digits(2 ** 1000)))
from digits import get_digits from fractions import Fraction from itertools import count def compute_continued_fraction(seq, limit): "Expand the given continued fration sequence to the nth convergent." a = seq.next() if limit == 1: result = Fraction(a) else: result = a + Fraction(1, compute_continued_fraction(seq, limit - 1)) return result def e_seq(): "The sequence of values given in the problem to compute e." yield 2 for n in count(2): if n % 3 == 0: yield n / 3 * 2 else: yield 1 if __name__ == '__main__': result = compute_continued_fraction(e_seq(), 100) print '100th convergent:', result print 'Numerator digit sum:', sum(get_digits(result.numerator))
Find the sum of digits in the numerator of the 100th convergent of the continued fraction for e. """ from digits import get_digits from fractions import Fraction from itertools import count def compute_continued_fraction(seq, limit): "Expand the given continued fration sequence to the nth convergent." a = seq.next() if limit == 1: result = Fraction(a) else: result = a + Fraction(1, compute_continued_fraction(seq, limit-1)) return result def e_seq(): "The sequence of values given in the problem to compute e." yield 2 for n in count(2): if n % 3 == 0: yield n/3 * 2 else: yield 1 if __name__ == '__main__': result = compute_continued_fraction(e_seq(), 100) print '100th convergent:', result print 'Numerator digit sum:', sum(get_digits(result.numerator))