Validating a power JavaScript


In this problem we have to validate that a given number is a power of n. If the given number is power of n then it should return the true value of it otherwise it should return false.

To check this kind of validation we generally use arithmetic operators like Modulus - %, Division - /, comparison operators like less than < or greater than >.

Lets understand this problem with an example.

True value: 

N = 27 
So 33 = 27 

False Value:
N = 28
So 33 != 28 

In the above examples when N = 27 so the value is power of 3 it will return true. But when N = 28 so here N is not a power of 3 in this case it will return false.

In mathematics, a power of 3 is a number of the form 3 to the power n where n is an integer – the result of exponentiation with number 3 as the base and integer n as the exponent.

Algorithm

The algorithm mentioned below will give a step by step process to solve the given problem to solve this problem.

For example if we had given a number 243 then we have to check that it is power of 3 or not, its precise algorithm as follows:

Step 1: Declare a function named isPowerOf3. This function takes a num as an argument and returns true if num is a power of 3 and false otherwise.

Step 2: Then first check whether num is less than 1, in this case it will not be a power of 3.

Step 3: Then check If num is greater than or equal to 1, it divides num by 3 continuously as long as num is divisible by 3.

Step 4: If num becomes equal to 1, then it is a power of 3 and the function returns true value. If the given num is not a power of 3, then it will become less than 1 or not equal to 1 after the loop, in which case the function returns false.

Step 5: In the next step we will define a variable called number and assign a value in it.

Step 6: Then calls isPowerOf3 function with the number variable as an argument and prints a message to the console whether the input number is a power of 3 or not.

Flow Chart

Example

// define a function to check power of 3

function isPowerOf3(num) {
  if (num & 1) {
   return false;
  }
  while (num % 3 === 0) {
   num /= 3;
  }
  return num === 1;
}

// number declaration

var number = 243;

// Check the given number is power of 3

if (isPowerOf3(number)) {
  console.log(`${number} is a power of 3`);
} else {
  console.log(`${number} is not a power of 3`);
}

Output

243 is a power of 3

In the above code we have declared a function called isPowerOf3() which is taking an argument called num. And this function will check whether the number is a power of three or not. So basically we have used arithmetic and comparison operators to solve this problem.

In this program you can also take input from the user and check that it returns true or false. In javascript we usually take input with the help of prompt().

Complexity

The time complexity of this program is O(log3 n), where n is the number. This is because the isPowerOf3 function is dividing num argument by 3 continuously till it becomes less than 1 or not divisible by 3. The loop executes each time, num is divided by 3, which usefully divides its value by 3. Since the loop continues until num is less than 1 or not divisible by 3, so the number of times the loop executes is proportional to the base 3 logarithm of num. Therefore, the time complexity of the program is O(log3 n).

Hence space complexity of this program is O(1), because it only uses a constant amount of memory to store the number, the loop counter, and the value of the isPowerOf3 method.

Conclusion

This is the basic idea behind solving this kind of problem. In the overall process we have used a function called isPowerOf3, arithmetic operators and comparison operators to solve the problem. And saw how to calculate the time and space complexity of the algorithm.

Updated on: 18-Aug-2023

87 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements