# Project Euler 29

Here's a link to the problem. Is there any way I could optimize/speed up this code, perhaps make it more Pythonic?

"""How many distinct terms are in the sequence
generated by a^b for 2 <= a <= 100
and 2 <= b <= 100?"""

import math
from timeit import default_timer as timer

def distinctPowers(a_max, b_max):
products = []
for a in range(2, a_max + 1):
for b in range(2, b_max + 1):
if a ** b not in products:
products.append(a ** b)
return len(products)

start = timer()
ans = distinctPowers(100, 100)
elapsed_time = (timer() - start) * 1000 # s --> ms

print "Found %r in %r ms." % (ans, elapsed_time)

• What would you like to know about your code? It appears correct to me. Are you trying to speed it up? – clutton Apr 19 '14 at 16:42
• On a styling note, mixedCase is almost never used in Python. "mixedCase is allowed only in contexts where that's already the prevailing style (e.g. threading.py), to retain backwards compatibility." See legacy.python.org/dev/peps/pep-0008 – Sahand Apr 19 '14 at 20:33

You should check the data structure that you use. You are currently using a list, checking if the power exists, and then doing it again when you append unique entries.

If you use a set then you can skip the check and do an add of the power. This is significantly faster... about 50 times if I calculated correctly. Your function would then look like this:

def distinctPowers(a_max, b_max):
products = set()
for a in range(2, a_max + 1):
for b in range(2, b_max + 1):

def distinctPowers(a_max, b_max):