Example #1
0
def problem_nine():
    """
    A Pythagorean triplet is a set of three natural numbers, a  b  c, for which,

    a^2 + b^2 = c^2
    For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2

    There exists exactly one Pythagorean triplet for which a + b + c = 1000.
    Find the product abc.
    """
    target = 1000
    combinations = [(a, b, target - b - a) for b in xrange(1, target / 2) for a in xrange(b, target / 2)]
    triples = filter(is_pythagorean, combinations)
    a, b, c = first(filter(lambda t: sum(t) == target, triples))
    return a * b * c
Example #2
0
def problem_12():
    """
    What is the value of the first triangle number to have over five
    hundred divisors?

    The sequence of triangle numbers is generated by
    adding the natural numbers. So the 7th triangle number would be 1 +
    2 + 3 + 4 + 5 + 6 + 7 = 28.

    Let us list the factors of the first seven triangle numbers:
     1: 1
     3: 1,3
     6: 1,2,3,6
    10: 1,2,5,10
    15: 1,3,5,15
    21: 1,3,7,21
    28: 1,2,4,7,14,28
    We can see that 28 is the first triangle number to have over five divisors.
    """
    num_factors = lambda x: reduce(op.mul, [e + 1 for (p,e) in factors(x)])
    return first((tri for tri in triangle_numbers() if num_factors(tri) > 500))