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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: