Home > Mobile >  Big O notation of Formula - P *R Squared
Big O notation of Formula - P *R Squared

Time:11-08

I want to calcuate the Big O notation for an area of circle.I wonder what is the time complexity of Pi *r^2? is it O(n) or O(n^2)? Please also check if my way of doing is correct.

 Algorithm
   Step 1: Start //  f(n)=O(1)  ( Executes 1 time)
   Step 2: Get an integer input from user for AREA and RADIUS//. f(n)=O(n)=O(1) ( Executes 1 time)
   Step 3: Print the statement "Enter the Radius of Circle: //f(n)=O(n)=O(1) ( Executes 1 time)
    Step 4: Calculate the area of the circle using the formula of  
    //f(n)=O(n)=?
    (r - Radius) //f(n)=O(n)=O(1) ( Executes 1 time)
    
   Step 5: Print the statement "Area of Circle:" //f(n)=O(n)=O(1) ( Executes 1 time)
    
   Step 6: Print Area//f(n)=O(n)=O(1) ( Executes 1 time)
    
   Step 7: Stop //f(n)=O(n)=O(1) ( Executes 1 time)
    
    f(n)=O(n)=?

CodePudding user response:

This depends on your model of computation. For most practical purposes its O(1), because the runtime does not depend on your input r. This assumes implicitly that your input is bounded, for example fits into a 32-bit integer.

More theoretical you can think about arbitrary big r. The N in the big O notation then is normally the length (number of bits) of r and not the size of the number. Here you cant assume that you can multiply any two numbers in O(1) but it will depend on the length of the numbers.

In this case you would get O(N^2) with a naive multiplying algorithm, which can be improved to O(N log N) with more advanced techniques. See https://en.wikipedia.org/wiki/Multiplication_algorithm#Computational_complexity_of_multiplication .

  • Related