0

There is an algorithm which requires us to print all subsequences of the prime factors of a number. For example, if the number is 6, we will print:

{}, {2}, {3}, {2 3}

Now the given constraint is that the prime factor of a number will not exceed 13.

The complexity is clearly O(pow(2, prime factors of the number))

Now, my doubt is: Since we know that the number of primes less than or equal to 13 is just 6. This makes the worst time complexity O(pow(2, 6)) which is clearly O(1). So, can we call the algorithm a constant time algo, since we already know all the constant values which are possible candidates for the time complexity?

1
  • 2
    But there are infinitely many numbers whose prime factors are all less than or equal to 13. Commented Aug 22, 2018 at 17:39

1 Answer 1

1

Not so fast.

If k = 2n2 * 3n3 * 5n5 * 7n7 * 11n11 * 13n13 then the number of subsequences is (n2+1) * (n3+1) * (n5+1) * (n7+1) * (n11+1) * (n13+1) and the average length of those sequences is (n2 + n3 + n5 + n7 + n11 + n13)/2 It is easy to put the bound that each of those terms is O(log(k)). And from that, you can put an upper limit of O(log(k)7) on the size of the output. (If pnp ≈ k1/6 for each p then all 6 terms are simultaneously of that scale.)

If the number does not start factored, you have to add in the difficulty of factoring the number first.

Sign up to request clarification or add additional context in comments.

2 Comments

Shouldn't the average length of those sequences be the sum of the n_i divided by 6 rather than 2? (there are 6 primes here). This of course doesn't change the conclusions of your answer; it is just side observation.
@LeandroCaniglia No, because that's the average exponent for a single prime across all sequences, and the exponents evenly from 0 to n_i. That averaging is where the "/2" comes from.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.