This article describes the prime factor decomposition operation implemented by Java. Share it for your reference, as follows:
Here we demonstrate that Java implements prime factor decomposition through recursion, the code is as follows:
import java.util.Scanner;public class Prime { @SuppressWarnings("resource") public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("(Wulin.com test results) Please enter the number to be decomposed:"); int num = scanner.nextInt(); PrimeSplit(num, 2); } /** * Get the prime factor* @param num The number to be decomposed* @param count=2 */ public static void PrimeSplit(int num, int count){ while( count < num && num % count != 0 ){ //Get the minimum prime divisor of this number count ++; } if(count < num){ System.out.print(count + "*"); //Get the minimum prime divisor of the quotient PrimeSplit(num/count, 2); }else{ //If the quotient and the minimum prime divisor are the same, it means it is itself and the loop ends. System.out.println(count ); } }}Running results:
PS: Here is an online tool with similar functions for your reference: Online decomposition factor calculator tool http://tools.VeVB.COM/jisuanqi/factor_calc
For more information about Java algorithms, readers who are interested in this site can view the topics: "Java Data Structure and Algorithm Tutorial", "Summary of Java Operation DOM Node Tips", "Summary of Java File and Directory Operation Tips" and "Summary of Java Cache Operation Tips"
I hope this article will be helpful to everyone's Java programming.