The integer into a product of a prime number

Title: A into a product of an integer prime number

(Prime number is 1 and except itself, no longer be an integer number which is divisible, such as: 2..3.5.7.11.13.17.19.23.39.31 ..............................
prime is a prime number. other than prime numbers are called bonded together such count: 4.6.8.16.32.64.72 ..........................................
Note, however, is neither a prime nor composite).

Mode 1:

import java.util.ArrayList;
import java.util.List;
import java.util.*;
public class test1 {

		    public static void main(String[] args) {
		    	Scanner scanner=new Scanner(System.in);
		    	 int n=scanner.nextInt();
		        serch(n);
		    }
		    public static void serch(int n){
		        List<Integer> list = new ArrayList<Integer>();
		        int i = 2;
		        int j = 0;
		        System.out.print(n+"=");
		        while (n >= 2) {
		            if (n % i == 0) {
		                list.add(i);
		                n /= i;
		                i = 2;
		            }else {
		                i++;
		            }
		        }
		        int t=list.size()-1;
		        for (Integer l : list){
		            System.out.print(l);
		            if (j<t) {
		            	 System.out.print("*");
		            	 j++;
					}
		            
		        }
	
		    }
}

 The input 120, the following results

Option 2:

import java.util.*;
public class test1 {

		    public static void main(String[] args) {
		    	Scanner scanner=new Scanner(System.in);
		    	 int n=scanner.nextInt();
		    	 primeCount(n);
		    }
		    public static void primeCount(int num) {
				StringBuffer sb = new StringBuffer();
				sb.append(num + "=");
				int minNumber = 2;// 定义最小的质数
				while (minNumber < num) {
					if (num % minNumber == 0) {
						num = num / minNumber;
						sb.append(minNumber + "*");
					} else {
						minNumber++;
					}
				}
				sb.append(minNumber);
				System.out.print(sb.toString());
			}
	}

Input 90, the following results 

 

 

Published 57 original articles · won praise 36 · views 60000 +

Guess you like

Origin blog.csdn.net/hqy1719239337/article/details/102499121