patternjavaMinor
Prints out all ways to multiply smaller integers that equal the original number
Viewed 0 times
thenumberallequalprintswaysoriginalmultiplysmallerthat
Problem
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:
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.
Here is the sample from one solution:
Example:
$ java -cp . PrintFactors 3232 * 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 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 >= 2; divisor--) {
if (dividend % divisor != 0)
continue;
if (divisor > 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 <= divisor) {
if (quotient <= 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);
}
}Solution
public static void printFactors(int number) {
if (number <= 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) ==> 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 >= 2; divisor--) {
if (dividend % divisor != 0) {
continue;
}
if (divisor > 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 <= divisor) {
if (quotient <= 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);
}Code Snippets
public static void printFactors(int number) {
if (number <= 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) ==> 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 >= 2; divisor--) {
if (dividend % divisor != 0) {
continue;
}
if (divisor > 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.
*/Context
StackExchange Code Review Q#42939, answer score: 3
Revisions (0)
No revisions yet.