Home > Back-end >  Returning Prime Factorization in a particular form
Returning Prime Factorization in a particular form

Time:11-22

My problem is that I need to take an inputted integer and return the prime factorization of it. It needs to be in the form of:

int * int * int * int

For example, the prime factorization of 180 would return:

2 * 2 * 3 * 3 * 5.

The code I have currently is:

public static String factor(int n)
{
    String str = "";
    if(isPrime(n)) {
        str = ""   n;
    }else if(n == 2) {
        str = "2";
    }else{
        for (int i = 2; i <= Math.sqrt(n); i  )
        {
            while (n % i == 0)
            {
                str = str   i;
            }
        }
    }
    return str;
}

The isPrime method mentioned is:

public static boolean isPrime(int n)
{
    if (n <= 1) {
        return false;
    }
    for (int i = 2; i <= n/2; i  )
    {
        if (n % i == 0) {
            return false;
        }
    }
    return true;
}

CodePudding user response:

In your while loop, you need to divide the remaining n value by each found factor. Otherwise, you'll never break out of the loop.

while (n % i == 0) {
    str  = i   " ";
    n /= i;
}

CodePudding user response:

Below is the Running Code.

public static void main(String[] args) throws IOException
       {
          System.out.println(factor(24));
}
           public static String factor(int n)
    {
        String str = "";
        String multiplySymbol = "";
        if(isPrime(n)) {
            str = ""   n;
            }else if(n == 2) {
            str = "2";
            }else{
            for (int i = 2; i <= n; i  )
            {
                while (n % i == 0)
                {
                  str  =  multiplySymbol i ;
                  multiplySymbol = "x";
                    n /= i;
                }
            }
        }
        return str;
    }
           
           public static boolean isPrime(int n)
    {
        if (n <= 1) {
        return false;
        }
        for (int i = 2; i <= n/2; i  )
        {
            if (n % i == 0) {
            return false;
            }
        }
        return true;
    }
      
}

CodePudding user response:

Looking at just the formatting problem, inserting the " * " separator as required. I approach it as "every number, except the first, is preceded by the separator string". This puts the separator before, rather than after, each entry, and automatically leaves the last entry without a trailing separator. I use a boolean flag to indicate the first entry in the list.

This is in pseudocode:

// primeArray holds the previously calculated prime factors.
function printPrimes(primeArray)
  separator ← " * "
  atFirst ← true

  foreach thisPrime in primeArray
    if (atFirst)
      // No separator before first prime.
      atFirst ← false
    else
      // Put separator before all the other primes.
      print(separator)
    end if

    // Print the number.
    print(thisPrime)
  end foreach
end function
  • Related