Prints out all ways to multiply smaller integers that equal the original number Write a program in Java that takes a positive integer and prints out all ways to multiply smaller integers that equal the original number, without repeating sets of factors.  In other words, if your output contains 4 * 3, you should not print out 3 * 4 again as that would be a repeating set.  Note that this is not asking for prime factorization only.


Here is the sample from one solution:

Example: 


$ java -cp . PrintFactors 32

  
  32 * 1
  
  16 * 2
  
  8 * 4
  
  8 * 2 * 2
  
  4 * 4 * 2
  
  4 * 2 * 2 * 2
  
  2 * 2 * 2 * 2 * 2


I'm looking for code review, best practices, optimizations etc.

Verifying Complexity: \$O(n!)\$ - time, where \$n\$ is the input number.



public final class PrintFactors {

    private PrintFactors() {}

    public static void printFactors(int number) {
        if (number &lt;= 0) throw new IllegalArgumentException("The number should be greater than 0.");
        printFactorsList(number, number + "*" + 1 + "\n", number);
    }

    private static void printFactorsList(int dividend, String factorString, int prevDivisor) {
        /*
         * This function contains factorString as an argument to facilitate the recursive call for subsequent
         * factors until it reaches prime values. For example, let's say input number = 32 and when i = 8 it prints
         * 8*(32/8) ==&gt; 8 * 4 but the subsequent reduction of 4 is needed and this is done by recursively passing in 4
         * as number. But we also want to maintain the chain "8 * ". So this makes the carry over string as an input
         * argument for the helper function printFactorsList
         */
        for (int divisor = dividend - 1; divisor &gt;= 2; divisor--) {

            if (dividend % divisor != 0)
                continue;

            if (divisor &gt; prevDivisor)
                continue;

            int quotient = dividend / divisor;

            /*
             * 32*1 16*2 8*4 8*2*2 4*4*2 4*2*2*2 2*2*2*2*2
             * 
             * Note: as we go right, the values keeps descending.
             */
            if (quotient &lt;= divisor) {
                if (quotient &lt;= prevDivisor) {
                    System.out.println(factorString + divisor + "*" + quotient);
                }
            }
            printFactorsList(quotient, factorString + divisor + "*", divisor);
        }
    }

    public static void main(String[] args) {
        printFactors(12);
        System.out.println();
        printFactors(32);
    }
}
public static void printFactors(int number) {
  if (number &lt;= 0) {


You should include the number into the exception's text.

    throw new IllegalArgumentException("The number should be greater than 0.");
  }
  printFactorsList(number, number + "*" + 1 + "\n", number);
}


That's a bad name for a function.

private static void printFactorsList(int dividend, String factorString, int prevDivisor) {


This should be formatted as javadoc.

      /*
       * This function contains factorString as an argument to facilitate the recursive call for subsequent
       * factors until it reaches prime values. For example, let's say input number = 32 and when i = 8 it prints
       * 8*(32/8) ==&gt; 8 * 4 but the subsequent reduction of 4 is needed and this is done by recursively passing in 4
       * as number. But we also want to maintain the chain "8 * ". So this makes the carry over string as an input
       * argument for the helper function printFactorsList
       */


Why not start this loop from prevDivisor?
Also, you can optimize this loop by storing numbers in factorized form and generating all divisors less than something on the fly. This will be much faster because you will iterate only over valid divisors. You can also memoize all solutions for a number and reuse them.

  for (int divisor = dividend - 1; divisor &gt;= 2; divisor--) {

    if (dividend % divisor != 0) {
      continue;
    }

    if (divisor &gt; prevDivisor) {
      continue;
    }

    int quotient = dividend / divisor;

          /*
           * 32*1 16*2 8*4 8*2*2 4*4*2 4*2*2*2 2*2*2*2*2
           *
           * Note: as we go right, the values keeps descending.
           */


Merge this two ifs into one.

    if (quotient &lt;= divisor) {
      if (quotient &lt;= prevDivisor) {
        System.out.println(factorString + divisor + "*" + quotient);
      }
    }
    printFactorsList(quotient, factorString + divisor + "*", divisor);
  }
}

public static void main(String[] args) {


You are not reading from anywhere. Is this the intended behavior?

  printFactors(32);
}