2

The perfect powers are numbers of the form xy with x1 and y>1. I'm interested in counting the exact number of perfect powers not greater than N. I'd like to ask if there's some method much better than enumerating all perfect powers in O(N)?

Share a link to this question
CC BY-SA 4.0
6

There's an obvious approach we can do - count the squares, the cubes, the fourth powers, and so on.

But then, we look closer. If we count 24 and 42, we've just counted 16 twice. All of the fourth powers are already squares; we've already counted them, so we should put zero weight on those fourth powers.
Then, the next place we run into trouble is the sixth powers. We can write 26=43=82; each sixth power is a square and a cube, already double-counted. We have to subtract the number of sixth powers to balance this.

Is there a pattern to the weights we need here? Why, yes. Let μ be the Möbius function of number theory. The count we seek is

# perf. powers{2,3,,N}=k2μ(k)(# perf. kth powers{2,3,,,N})
This works because, for any m>1, the sum d|mμ(d) is zero. So then, if n is a "primitive" mth power, it's also a dth power for each d dividing n and no others. We get μ(d) for each of these, except we leave off the last μ(1)=1 term since we don't count first powers. Excluding that 1 leaves us with a sum of 1, and we've counted each perfect power exactly once.

Now, how many perfect kth powers are there in {2,3,,N}? There are N1/k1, which is positive as long as 2kN. From that, we get a formula:

# perf. powers{2,3,,N}=k=2log2Nμ(k)(N1/k1)
That method is O(logN). Much better.

Addendum: The original statement xy with x1 includes 1 as a perfect power. To include that, just add 1 to the count.

Share a link to this answer
CC BY-SA 4.0
2

Using inclusion-exclusion, the number of perfect powers up to N is

1k=2log2Nμ(k)N1/k1,
where μ is the Möbius function. (Note that the restriction of the upper limit of the sum to log2N requires that some care be taken to ensure that 1 is counted exactly once.)

Here's a worked example for N=1000:

kμ(k)N1/k12130319404512612711801901
Thus the number of perfect powers up to 1000 is 1((30)+(9)+(2)+2+(1))=41.

To check your results, you can compare with sequence A089579, which gives the number of perfect powers, excluding 1, less than 10n. Thus its third entry is 39, as both 1 and 1000 are not counted.

Share a link to this answer
CC BY-SA 4.0

Not the answer you're looking for? Browse other questions tagged or ask your own question.