This article describes the method of Java to implement integer decomposition of prime factors. Share it for your reference, as follows:
Question content:
Each non-prime number (combination) can be written as a form of multiplying several prime numbers (also called prime numbers), and these prime numbers are all called the prime factors of this composite number.
For example, 6 can be decomposed to 2x3, while 24 can be decomposed to 2x2x2x3.
Now, your program needs to read an integer in the range [2,100,000] and then output its prime factor decomposition; when what you read is a prime number, output it itself.
Input format:
An integer with a range of [2,100,000].
Output format:
As shown in:
n=axbxcxd
or
n=n
There are no spaces between all symbols, x is the lowercase letter x.
Enter a sample:
18
Output sample:
18=2x3x3
Code example:
import java.util.Scanner;public class Main { public static boolean isPrime(int i) { boolean isPrime = true; //Separate to the square root of i to judge for (int j = 2; j<=Math.sqrt(i);j++) { if(i%j==0) isPrime = false; } return isPrime; } public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("Wulin.com-decomposition factor test: "); Scanner in = new Scanner(System.in); int n = in.nextInt(); String out = n + "="; if(isPrime(n)) { out = out+ n; } else { while(n!=1) { for(int j=2;j<=n;j++) { //Special processing for the last one if(j==n) { n=1; out = out + j; break; } if(n%j==0) { n=n/j; out = out + j+"x"; break; } } } } System.out.println(out); in.close(); }}Running results:
PS: Here are a few calculation tools for you to refer to:
Online decomposition of mass factor calculator tools:
http://tools.VeVB.COM/jisuanqi/factor_calc
Online unary function (eq) solution calculation tool:
http://tools.VeVB.COM/jisuanqi/equ_jisuanqi
Scientific Calculator Online Use_Advanced Calculator Online Calculator:
http://tools.VeVB.COM/jisuanqi/jsqkeexue
Online Calculator_Standard Calculator:
http://tools.VeVB.COM/jisuanqi/jsq
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.