Java decomposition prime factor, java prime factor
1 package test; 2 3 import java. util. detail; 4 5 public class Test19 {6/** 7 * Analysis: For the decomposition of n, we should first find a minimum prime number k 8 9 * Minimum prime number: "2 ". 2 is the smallest prime number, that is, the even number and the prime number, and then follow the steps below: 10 11 * (1) if the prime number is equal to n, it indicates that the process of decomposing the prime factor has ended, print it out. 12 13 * (2) if n> k, but n can be divisible by k, then the value of k should be printed and divided by the quotient of n, as the new positive integer you n, repeat the first step. 14 15 * (3) if n cannot be divisible by k, k + 1 is used as the value of k and the first step is repeated. 16*17 * @ param think 18 */19 public static void main (String [] args) {20 seconds s = new seconds (System. in); 21 System. out. println ("type a positive integer:"); 22 int n = s. nextInt (); 23 int k = 2; // defines a minimum prime factor of 24 System. out. print (n + "="); 25 while (k <= n) {// cyclically judge 26 if (k = n) {27 System. out. print (n); 28 break; 29} else if (n % k = 0) {30 System. out. print (k + "*"); 31 n = n/k; 32} else k ++; 33} 34} 35}