Title: Decompose a positive integer into factorization. For example: Enter 90 and print out 90=2*3*3*5.
In order to be familiar with strengthening the basic exercises, engage in the classic small demo. Not much to say, directly paste the code, look at the comments. Package www.test; import Java.util.Scanner;
public class Resovle {public static void Main (string[] args) {/**
Simple analysis: * If this prime number is exactly equal to N, then the process of decomposing the factorization is over and printing can be done. If n can be divisible by K, the value of k should be printed, and the quotient of n divided by K, as the new positive integer you n, repeat the first step. If n cannot be divisible by K, the first step is repeated with k+1 as the value of K. * */
Scanner sc = new Scanner (system.in); Created the keyboard entry object System.out.println ("Enter the integer to decompose:"); int num = Sc.nextint (); System.out.print (num+ "="); In order to output the format: 90=2*3*3*5. Resolve (num); Sc.close (); Remember to close the stream}
/**
* Decomposition Method
*
*/
public static void Resolve (Int. num) {for (int i = 2; i < num; i+ +) {if (num% i = = 0) {System.out.print (i+ "*");//Call method, determine whether num/i is prime (prime number), is the prime number of direct output if (num/i >0 && isprime (num/i)) { System.out.print (num/i + "");
//Is not a prime number, can also be decomposed, recursive call (call yourself);}else{resolve (num/i);} Break;}}} Determines whether the prime number is private static Boolean isprime (int num) {for (int i = 2; i < num; i++) {if (num% i = = 0) {return false;}} return true;}}
Java Decomposition Factorization Foundation enhancements