@@ -0,0 +1,1536 @@
+#!/usr/bin/env python%0A#-*-coding:utf-8-*-%0A%0A'''%0AThe sequence of triangle numbers is generated by adding the natural numbers.%0ASo the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28.%0AThe first ten terms would be:%0A1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...%0ALet us list the factors of the first seven triangle numbers:%0A%0A 1: 1%0A 3: 1,3%0A 6: 1,2,3,6%0A10: 1,2,5,10%0A15: 1,3,5,15%0A21: 1,3,7,21%0A28: 1,2,4,7,14,28%0A%0AWe can see that 28 is the first triangle number to have over five divisors.%0AWhat is the value of the first triangle number to have over five hundred divisors?%0A'''%0A%0Aimport timeit%0Aimport math%0A%0A# very slow%0Adef loop(n):%0A    c = 1 # count%0A    t = 0 # triangle number%0A    while True:%0A        t += c%0A        d = get_divisor(t)%0A        if d %3E= n:%0A            return t%0A        else:%0A            c += 1%0A%0A# faster%0A# http://tsumuji.cocolog-nifty.com/tsumuji/2010/01/project-euler-2.html%0Adef factorize(n):%0A    c = 1%0A    p = 1%0A    while True:%0A        p_next = get_divisor((c+1)/2) if (c+1) %25 2 == 0 else get_divisor(c+1)%0A        if p*p_next %3E n:%0A            return c*(c+1)/2%0A        else:%0A            c += 1%0A            p = p_next%0A%0Adef get_divisor(n):%0A    d = 0%0A    for i in range(1, int(math.sqrt(n)+1)):%0A        if i*i == n:%0A            d += 1%0A        elif n %25 i == 0:%0A            d += 2%0A    return d%0A%0Aif __name__ == '__main__':%0A    print loop(500)%0A    print factorize(500)%0A    print timeit.Timer('problem_012.loop(500)', 'import problem_012').timeit(1)%0A    print timeit.Timer('problem_012.factorize(500)', 'import problem_012').timeit(1)%0A

add problem 12