1

I am trying to work out the time complexity of my function but I'm not sure how it works with powers. I've got $y=(\frac{x}{3})^n$ as one of my lines. How many basic operations is this? Would it be $2(n-1)$ for the power then $1$ for the division and $1$ for the assignment of $y$?

Also, is the square root of a number just $1$ basic operation?

phan801
  • 616
  • 5
  • 10
Maths
  • 11
  • 1
  • 1
    The power operation can be done in $O(\log n)$ time using repeated squaring. The division and assignment operations are indeed $O(1)$ complexity. – Inuyasha Yagami May 29 '21 at 18:16
  • 1
    @InuyashaYagami Actually, it is $O(\log n)$ multiplications, but depending on the size of $x$, each multiplication (and other arithmetic operations) may not be in $O(1)$. – Nathaniel May 29 '21 at 18:19
  • @Nathaniel Yes, you are right!. I was assuming a multiplication operation to be $O(1)$ time irrespective of the size of $x$ assuming RAM model (details here). But a more detailed analysis of what you are saying is probably here. :p – Inuyasha Yagami May 29 '21 at 18:21
  • Ahh great thank you! Do you know what the time complexity of a square root would be? – Maths May 29 '21 at 18:59

1 Answers1

1

You get to pick your model of computation. If you intend to run your algorithm on a computer, then it is appropriate to pick the model of computation that best fits the way your algorithm will be implemented. For example, in floating point arithmetic, computing $(x/3)^n$ takes $O(1)$, whereas in rational arithmetic you use the repeated squaring algorithm, but since the numbers involved get large, the running time isn't quite $O(\log n)$ (in particular, the end result is $\Omega(n)$ bits long). In practice, if we are computing $(x/3)^n$ exactly, then we're probably using modular arithmetic (i.e., computing everything modulo some number $m$), in which case the running time is $O(\log n \cdot \log m)$ bit operations.

In theoretical work, the most common model is some form of the RAM machine, or possibly the real RAM, which is common in computational geometry. In both cases, there isn't really a well-defined model that everybody agrees on, but in most cases it makes no difference. Analyzing an algorithm is model is important so we can compare the asymptotic running time of different algorithms.

Yuval Filmus
  • 276,994
  • 27
  • 311
  • 503