def test_triangle_numbers(self): t = triangle_number() self.assertEquals(t.next(), 1) self.assertEquals(t.next(), 3) self.assertEquals(t.next(), 6) self.assertEquals(t.next(), 10) self.assertEquals(t.next(), 15)
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. What is the value of the first triangle number to have over five hundred divisors? ''' from math import sqrt from triangle_numbers import triangle_number if __name__ == '__main__': triangle_numbers = triangle_number() t = triangle_numbers.next() divisors = 0 while divisors <= 250: t = triangle_numbers.next() divisor_bound = int(sqrt(t)) + 1 # Since divisors come in pairs, we can just find all the divisors <= sqrt(t) # In this case we only need to check for 250 divisors = len([n for n in xrange(1, divisor_bound, 1) if t % n == 0]) print t