Tag Archives: set

Problem Twenty Nine

5 Dec

Here’s problem twenty nine:

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

This is remarkably simple in Python.

Here it is:

result = {a**b for a in range(2, 101) for b in range(2, 101)}
print(len(result))

A set is used to keep track of unique elements (note the curly braces), and a list comprehension with two for loops are used to generate the values. The length of the set is printed at the end.

Answer: 9183