1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
from itertools import count triangles = 0 # Trix's age for age in count(1): # the sequence of visible triangles with row # number is 1, 4, 10, 20, 35, ... which are # known as tetrahedral numbers triangles += age * (age + 1) // 2 # if this is divisible by 5, 7, 8 and 9 it will # be divisible by all the digits from 1 to 9 if not triangles % (5 * 7 * 8 * 9): print('Trix is {} ({} triangles).'.format(age, triangles)) break |