Consider all integer combinations of ab for 2 ≤ a ≤ 5 and 2 ≤ b ≤ 5:

22=4, 23=8, 24=16, 25=32
32=9, 33=27, 34=81, 35=243
42=16, 43=64, 44=256, 45=1024
52=25, 53=125, 54=625, 55=3125

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 ab for 2 ≤ a ≤ 100 and 2 ≤ b ≤ 100?


In [1]:
from euler import canonical_decomposition, canonical_decomposition2, get_factors, get_factors2

In [2]:
canonical_decomposition(12)


Out[2]:
{2: 2, 3: 1}

In [3]:
canonical_decomposition2(12)


Out[3]:
{2: 2, 3: 1}

In [4]:
get_factors(12)


Out[4]:
[2, 2, 3]

In [5]:
tuple(sorted(get_factors(12) * 5))


Out[5]:
(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3)

In [6]:
def foo(n):
    dn = set([])
    for a in range(2, n + 1):
        af = get_factors(a)
        for b in range(2, n + 1):
            dn.add(tuple(sorted(af * b)))
    return len(dn)

In [7]:
n = 100
%timeit foo(n)
foo(n)


1 loops, best of 3: 232 ms per loop
Out[7]:
9183

In [8]:
def foo(n):
    return len(set([a ** b for a in range(2, n + 1) for b in range(2, n + 1)]))

In [9]:
n = 100
%timeit foo(n)
foo(n)


10 loops, best of 3: 25.5 ms per loop
Out[9]:
9183