#29 - Distinct powers

Consider all integer combinations of $a^b$ for $2\leq a\leq 5$ and $2\leq b\leq 5$:

$a^b$2345
2481632
392781243
416642561024
5251256253125

If they are then placed in numerical order, with any repeats removed, we get the following sequence of 15 distinct terms:

4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125

How many distinct terms are in the sequence generated by $a^b$ for $2\leq a\leq 100$ and $2\leq b\leq 100$?


We can solve this through brute force directly. The set() structure automatically removes duplicates for us. A simple for loop suffices:

# file: "problem029.py"
s = set()

for a in range(2, 101):
    for b in range(2, 101):
        s.add(a ** b)

print(len(s))

Running the short loop produces an output of,

9183
0.02056965463349574 seconds.

Therefore, there are 9183 distinct terms in the sequence.