The goal of this lab is to write a class Factoring for computing the prime factorization of an integer N >= 2 received from the user. The prime factoring of an integer N is the series of prime numbers in the nondecreasing order whose product is equals to N. Here are some examples of prime factorization:
If a prime divides N more than once, the number of times that the prime divides N can be presented as an exponent, as in:
The prime factorization of an integer N can be found dynamically using an algorithm that searches for prime factors in increasing order and, when a prime factor is found, reduces N by dividing the prime factor as many times as possible. More precisely, we can use the following algorithm:
Suppose N is the input and P is a prime factor of N (P may be N itself). We can argue that the following properties hold:
This argument applies not only to N and its prime factor P but also all the values of theDivisor and its prime factor at the start of Step 2a.
From this we can conclude that if the algorithm finds that the value of theDivisor divides theQuotient, it must be the case that the value is prime, and so, the algorithm discovers all the prime factors of N in the increasing order. In particular, if theQuotient is prime, its divisor (theQuotient itself) is found when theDivisor is increased theQuotient.
Each of the two different output formats of prime factorization can be generated by implementing this algorithm and by adding a component to produce the output. In the first format, we have only to produce an asterisk before printing the prime factor, but that asterisk sign is not needed if theQuotient is equal to the input N. In the second format, instead of printing each prime factor, we compute the number of times the prime factor that has been found divides theQuotient and prints the count after a caret (‘^’) if the number is greater than or equal to 2.
Here is an example execution:
You may use this template: Factoring.java