For a given number N, you do a loop over the potential base numbers A, and calculate the exponent P, checking whether it's integer. This way, you have to loop over a lot of numbers.
If you do it the other way round, with a loop over the potential exponents P, calculating the matching base A and checking that for an integer value, you only have to iterate from P=2 to P=30.
I didn't check if your code works - I have my doubts, especially with the "check for integer" part.
A few comments on your coding style:
Your method isPower()
is meant to check if the given number can be represented as a power A^P. Instead of an int
you should return a boolean
(true or false instead of 1 or 0).
The naming of classes, methods, fields, variables and so on is crucial for the readability of code. They should reflect the thing they represent. So, isPower
is a good name for a method that checks whether the number is a power. But names like variable
don't help. Names like exp
and base
are good choices, but you only declare them and don't use them (any decent IDE will flag them as unused). One-letter names should be avoided as much as possible (with the exception of loop variables like i
, j
, or k
).
There are naming conventions in Java, especially variables should always begin with a lowercase letter. Long-term Java developers will automatically understand anything that begins with uppercase as a class name, and have a hard time if in your code, the conventions don't apply.
Your indentation and placement of braces is unique. Following the usual style makes it easier to read your code and doesn't waste so much space (even the simple class presented here has lines too wide to fit into the space on this site).
Code like
if( A == 1)
return 1;
not surrounding the dependent statement with braces, is a maintenance risk. Imagine that later someone wants to add another statement here:
if( A == 1)
log.info("found a solution");
return 1;
That looks fine at a first glance, but effectively it's:
if( A == 1) {
log.info("found a solution");
}
return 1;
Not what you want, isn't it? So, I recommend to always use braces.
Write Javadoc comments at least for all public classes and methods.
After renaming, commenting and reformatting your code (but keeping your algorithm), we'd get:
/**
* Solution to the <a href=https://www.interviewbit.com/problems/power-of-two-integers/>
* Power of Two Integers</a> problem.
*
* @author your name
*/
public class Solution {
/**
* Check if <code>power</code> can be represented as <code>A^P</code>
* with A and P being integers, and P greater that 1.
* @param power the number under test
* @return true if <code>power</code> can be represented as <code>A^P</code>
*/
public boolean isPower(int power) {
if (power == 1) {
return true;
}
if (power % 2 == 0) {
for (int base = 2; base < power; base += 2) {
double exp = Math.log(power) / Math.log(base);
double formattedExp = Double.parseDouble(String.format("%.6f", exp));
if ((formattedExp == Math.floor(formattedExp)) && !Double.isInfinite(formattedExp)) {
return true;
}
}
return false;
} else {
for (int base = 3; base < power / 2; base += 2) {
double exp = Math.log(power) / Math.log(base);
double formattedExp = Double.parseDouble(String.format("%.6f", exp));
if ((formattedExp == Math.floor(formattedExp)) && !Double.isInfinite(formattedExp)) {
return true;
}
}
return false;
}
}
}