Problem Description The detailed description of this problem
Problem Description
The detailed description of this problem comes from the Programming Projects P6.3 that is in the book (page 302). Ignore the last paragraph that starts Use a class FactorGenerator...
You are writing a program that prints out all the factors of a number, in ascending order. All the work of the program, as usual, will be in Factors.java. In public static void main you should have the user enter a single integer. Then pass that integer to a method you write, public static String findFactors(int number). In that method you will find all the factors, in ascending order, and add them to a single string with a singe space between each number, including the last. Then return that string from the method.
Hints:
The parameter passed on the command line will always be integers greater than or equal to 2.
Don\'t forget about integer division and modulo!
Getting Started
We are going to do this exercise by writing code that prints the factors of a given number in Factors.java. How to Start Every Project in this Class create a source file called Factors.java. This is where your code will go.
There is no code to copy for the assignment. You get to do it all! Don\'t forget to provide proper Javadoc documentation.
To run your own tests while developing, in main ask the user for input (using a scanner) and outputs the factors sequentially. If you input 150 then your output should look like this:
Notice that the numbers are in ascending order. You will need to do that too.
Solution
public class Factors {
public static void main(String[] args) {
// command-line argument
long n = Long.parseLong(args[0]);
System.out.print(\"The prime factorization of \" + n + \" is: \");
// for each potential factor
for (long factor = 2; factor*factor <= n; factor++) {
// if factor is a factor of n, repeatedly divide it out
while (n % factor == 0) {
System.out.print(factor + \" \");
n = n / factor;
}
}
// if biggest factor occurs only once, n > 1
if (n > 1) System.out.println(n);
else System.out.println();
}
}

