title: How to determine a Perfect Power efficiently?
body: <p>Challenge: <a href="https://www.codewars.com/kata/57c7930dfa9fc5f0e30009eb/train/javascript" rel="nofollow noreferrer">https://www.codewars.com/kata/57c7930dfa9fc5f0e30009eb/train/javascript</a></p>
  <p>Hi I have been trying this problem for many hours but unfortunately my code is taking too long to pass:</p>
  <pre><code>function closestPower(num) {
    num = Math.floor(num);
    if (num &lt; 4) return 4;
  // check if input is perfect power
    let base = 2;
    while (base &lt; 10) {
    let exponent = Math.trunc(getBaseLog(base , num));
    if ( Math.pow(base, exponent)  === num ) { 
    return num;
  }
  base++;
    }
  // check for upper and lower
    base = 2;
    const verifyObj = {upper:null, lower:null}; // verify
    let upperPower = num + 1;
    let lowerPower = num - 1;
    while (!verifyObj.upper || !verifyObj.lower)
    {
      // no perfect power
      if (lowerPower &lt;= 2 ) verifyObj.lower = &quot;Not found&quot;;
      if (upperPower === Infinity ) verifyObj.upper = &quot;Not found&quot;;
    // up til base 9
    if (base === 10) { 
      if (!verifyObj.upper) upperPower++;
      if (!verifyObj.lower) lowerPower--;
      base = 2;
    }
  // upper
  if (!verifyObj.upper) {
    let exponent = Math.trunc(getBaseLog(base , upperPower));
    if ( Math.pow(base, exponent)  === upperPower ) { 
    verifyObj.upper = upperPower;
  }
  }
  // lower
  if (!verifyObj.lower) { 
    let exponent = Math.trunc(getBaseLog(base , lowerPower));
    if ( Math.pow(base, exponent)  === lowerPower ) { 
    verifyObj.lower = lowerPower;
  }
  }
  base++;
    }
    console.log(verifyObj) // {upper:64, lower: 49}
    // nearest power
    if ((upperPower - num) &lt; (num - lowerPower)) { 
      return upperPower;
    }
    else return lowerPower;
  }
  
  closestPower(56.5); // 49
  
  function getBaseLog(x, y) {
    return Math.log(y) / Math.log(x);
  }
  </code></pre>
  <p>I realized that my code is redundant as all i need to know if a “base” and “exponent” are more than 1 to determine a perfect power. Any formulas or ideas?</p>