Validating a power JavaScript

In JavaScript, validating whether a number is a power of another number (like 3) is a common programming problem. We need to check if a given number can be expressed as nk where n is our base and k is a non-negative integer.

To solve this validation, we use arithmetic operators like modulus (%), division (/), and comparison operators. The key insight is that powers of a number can only be divided by that base number repeatedly until we reach 1.

Understanding the Problem

Let's look at some examples to understand when a number is a power of 3:

// Powers of 3: 1, 3, 9, 27, 81, 243...
console.log("3^0 =", Math.pow(3, 0)); // 1
console.log("3^1 =", Math.pow(3, 1)); // 3  
console.log("3^2 =", Math.pow(3, 2)); // 9
console.log("3^3 =", Math.pow(3, 3)); // 27
console.log("3^5 =", Math.pow(3, 5)); // 243
3^0 = 1
3^1 = 3
3^2 = 9
3^3 = 27
3^5 = 243

Algorithm Approach

The algorithm works by continuously dividing the number by 3 while it remains divisible:

Step 1: Check if the number is less than 1 (not a valid power)

Step 2: Divide the number by 3 repeatedly while divisible

Step 3: If we reach exactly 1, it's a power of 3

Step 4: Otherwise, it's not a power of 3

Implementation

function isPowerOf3(num) {
  // Handle edge cases
  if (num < 1) {
    return false;
  }
  
  // Keep dividing by 3 while possible
  while (num % 3 === 0) {
    num /= 3;
  }
  
  // If we reached 1, it's a power of 3
  return num === 1;
}

// Test with different numbers
let testNumbers = [1, 3, 9, 27, 28, 81, 243, 244];

testNumbers.forEach(number => {
  if (isPowerOf3(number)) {
    console.log(`${number} is a power of 3`);
  } else {
    console.log(`${number} is not a power of 3`);
  }
});
1 is a power of 3
3 is a power of 3
9 is a power of 3
27 is a power of 3
28 is not a power of 3
81 is a power of 3
243 is a power of 3
244 is not a power of 3

Alternative Approach Using Logarithms

function isPowerOf3Logarithmic(num) {
  if (num < 1) return false;
  
  // Calculate log base 3 of num
  let logResult = Math.log(num) / Math.log(3);
  
  // Check if result is close to an integer
  return Math.abs(logResult - Math.round(logResult)) < 1e-10;
}

// Test the logarithmic approach
console.log("Using logarithmic approach:");
console.log("243 is power of 3:", isPowerOf3Logarithmic(243));
console.log("244 is power of 3:", isPowerOf3Logarithmic(244));
Using logarithmic approach:
243 is power of 3: true
244 is power of 3: false

Generalized Function for Any Base

function isPowerOfBase(num, base) {
  if (num < 1 || base < 2) {
    return false;
  }
  
  while (num % base === 0) {
    num /= base;
  }
  
  return num === 1;
}

// Test with different bases
console.log("16 is power of 2:", isPowerOfBase(16, 2)); // true (2^4)
console.log("25 is power of 5:", isPowerOfBase(25, 5)); // true (5^2)  
console.log("30 is power of 3:", isPowerOfBase(30, 3)); // false
16 is power of 2: true
25 is power of 5: true
30 is power of 3: false

Complexity Analysis

Time Complexity: O(log? n) where n is the input number. The while loop executes at most log? n times since we divide by 3 in each iteration.

Space Complexity: O(1) as we only use a constant amount of extra space regardless of input size.

Conclusion

Validating powers in JavaScript can be efficiently done using division-based approach with O(log n) time complexity. The algorithm repeatedly divides by the base until reaching 1 or finding the number isn't evenly divisible, making it reliable for power validation.

Updated on: 2026-03-15T23:19:00+05:30

247 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements