Ask Your Question

Revision history [back]

You can use Sage's Partitions constructor together with parts_in. First construct the kth powers which are at most n:

def powers_list(n,k):
    list of kth powers which are at most n
    # borrowed from Max Alekseyev's answer:
    return [m**k for m in range(1, 1 + n.nth_root(k,truncate_mode=1)[0])]

Then you can do

Partitions(300, parts_in=powers_list(300, 3))

to get the partitions of 300 made up of perfect cubes.

In a few timing tests, this is about the same speed as the other answer. For example:

sage: %time len(list(Partitions(300, parts_in=powers_list(300, 2))))
CPU times: user 2.47 s, sys: 58 ms, total: 2.52 s
Wall time: 2.53 s

sage: %time len(list(partitions_into_kth_powers(300, 2)))
CPU times: user 2.25 s, sys: 45.9 ms, total: 2.29 s
Wall time: 2.3 s