Title: Decompose a positive integer into factorization. For example: Enter 90 and print out 90=2*3*3*5.
Analysis: To decompose n factorization, you should first find a minimum prime number k, and then complete the following steps:
(1) If the prime number is exactly equal to N, then the process of decomposing the factorization is finished and printed out.
(2) if n>k, but n can be divisible by K, the value of k should be printed, and n divided by the quotient of K, as a new positive integer you n, repeat the first step.
(3) If n cannot be divisible by K, the first step is repeated with k+1 as the value of K.
public class Resolveprime {public static void main (string[] args) {System.out.println (Resolveprime (90)); System.out.println (Resolveprime (134)); System.out.println (Resolveprime (81));} /** * Decomposition factorization * @param num to be decomposed number * @return decomposed numeric result */public static string resolveprime (int num) {//define result string cache object, used to save result character St Ringbuffer sb = new StringBuffer (num + "=");//define the minimum prime number int i = 2;//to divide the method while (I <= num) {//If num can divide I, then I is a result of num number if (num% i = = 0) {//Save I to SB and follow *sb.append (i + "*");//Assign num divided by the value of I to Numnum = num/i;//Reset I to 2i = 2;} else {// If it is not divisible, then I self-increment i++;}} Removes the last * of the string cache object, returning the result to return sb.tostring (). substring (0, sb.tostring (). Length ()-1);}}
Output results
90=2*3*3*5134=2*6781=3*3*3*3
Java decomposition factorization