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){
                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 + " ");

      [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))

          return n;



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.

6 Years
Discussion Span
Last Post by Zaad

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


function in java

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.