Using a low priority queue, with one entry per capacity, is a smart way to list numbers. See the following python code.
import Queue # in Python 3 say: queue pmax, vmax = 10, 150 Q=Queue.PriorityQueue(pmax) p = 2 for e in range(2,pmax): p *= 2 Q.put((p,2,e)) print 1,1,2 while not Q.empty(): (v, b, e) = Q.get() if v < vmax: print v, b, e b += 1 Q.put((b**e, b, e))
With pmax, vmax, as in the code above, it produces the following output. For the proposed problem, replace pmax and vmax with 64 and 2**64 .
1 1 2 4 2 2 8 2 3 9 3 2 16 2 4 16 4 2 25 5 2 27 3 3 32 2 5 36 6 2 49 7 2 64 2 6 64 4 3 64 8 2 81 3 4 81 9 2 100 10 2 121 11 2 125 5 3 128 2 7 144 12 2
The complexity of this method is O (vmax ^ 0.5 * log (pmax)). This is due to the fact that the number of perfect squares dominates the number of perfect cubes, fourth degrees, etc., And for each square we do O (log (pmax)) work for get and put queue operations. For higher degrees, we do O (log (pmax)) when calculating b**e .
When vmax,pmax =64, 2**64 , operations of the order of 2 * (2 ^ 32 + 2 ^ 21 + 2 ^ 16 + 2 ^ 12 + ...) will be performed, i.e. order of order 2 ^ 33 of the queue.
Added note: this note discusses comment cf16, "only one remark, I do not think that the number of perfect squares is dominant in the number of perfect cubes, fourth degrees, etc.", they are all infinite, but yes, if we consider a finite set ". Itās true that in the general mathematical scheme of things, the powers are the same. That is, if P(j) is the set of all j 'th degrees of integers, then the power P(j) == P(k) for all integers j,k > 0 Elements of any two sets of degrees can be placed in 1-1 with each other.
However, when calculating perfect degrees in ascending order, no matter how many calculated, finite or not, the work of delivering squares dominates that for any other force. For any given x, the density of perfect k th degrees in the region x decreases exponentially with increasing k. With increasing x, the density of perfect k th degrees in the region x is proportional to (x 1 / k ) / x, therefore, third degrees, fourth degrees, etc. become vanishingly rare compared to squares as x increases.
As a concrete example, among the perfect forces between 1e8 and 1e9, the number (2; 3; 4; 5; 6) of the degree is about (21622; 535; 77; 24; 10). There are more than 30 times more squares between 1e8 and 1e9 than cases of higher powers than squares. Here are the ratios of the number of perfect squares between two numbers, as well as the number of higher perfect degrees: 10¹ā°-10¹āµ, rā301; 10¹āµ-10²ā°, rā2K; 10²ā°-10²āµ, rā15K; 10 ²-10ā°ā°, rā100K. In short, as x increases, the squares dominate more and more when perfect powers are transmitted in increasing order.