Hi,
I'm having an issue with the following directions:

public static int aPower(int a)
Given a number a > 1, Returns a number n such that for some m > 1 we have a = n ^ m. If no such n exists it returns 0

public static int aPowerOf2(int n)
Given a number n > 0, Returns a number m such that n = 2^m.
If no such m exists it returns -1

My code looks like this:

``````public class Server {

public static void main(String [ ] args){

}

public static int gcd(int a, int b)

{

int gcd = 0;
for (int i = 2; i<Math.min(a,b); i++){
if (a%i == 0 && b%i==0){
gcd = i;
}
}
return gcd;
}

public static int maxFactor(int a, int b)
{
int prime = 0;
for (int i = 2; i<Math.min(a,b); i++)
{
if( a%i==0 && b%i==0){
int num = i;

for (int x=2; x<num; x++)
{
int n = num%x;
if (n==0){
break;
}
prime = i;
}
}
}

return prime;
}

public static void Fibonacci(int n)
{
int f1= 1;
int f2= 2;
int f3= 3;
int a=0,b=1;

for (int i=0;i<n;i++){
System.out.print(a + " ");
a=a+b;
b=a-b;
}
}

[B]   public static int aPower(int a, int n, int m)
{
assert (a>1);
double srnum;

srnum = Math.sqrt(a);
if (a * a == srnum){
System.out.println("This is a perfect square.");
}else if (a * a != srnum){
System.out.println("This is not a perfect square.");

if (a == Math.pow(n, m))

System.out.print(n);

return n;

}

}[/B]``````

It is the part in BOLD that I have an issue with. For the aPower method, I cannot seem to find a program that follows the directions above. What I have so far only checks if 'a' is a perfect square, where the directions want to know what 'n' is if a = n^m, which is a lot more complicated than my coding.

Given a number n > 0, Returns a number m such that n = 2^m.

n = 2^m

For the calculation of m it can be taken as follows

log(n) = log(2^m)
log(n) = m log (2)
Therefore m = log (n)/log (2)

for the log you could use

``Math.log(x)``

function in java

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.