If you're not familiar with Car Talk , it's a horrible talk radio show hosted by Tom and Ray Magliozzi (aka Click and Clack the Tappet brothers). Every week there is a new puzzler and every so often it's math oriented. After hearing this week's mathy puzzler, I thought it would be fun to dig into the archives ( 2009 and 2008 ) and try solving a few of them in Python. Here's what I came up with: # 2008-10-20 The Perfect Square Dance! import itertools, math for numbers in itertools.permutations(range(1, 19)): for a, b in zip(numbers[::2], numbers[1::2]): sqrt = math.sqrt(a + b) if not str(sqrt).endswith('.0'): break if a == 1: sallys_partner = b elif b == 1: sallys_partner = a else: print 'Sally danced with %d' % sallys_partner break # 2008-10-27 One is the Magic Number print ''.join(str(n) for n in xrange(0, 1000000)).count('1') # 2008-12-08 It's Math Time! for ab in xrange(10, 100): ...
programming, electronics, photography, and tinkering