Decomposing prime factor in java

Keywords: Java

Content:

Every non prime number (sum) can be written in the form of multiplication of several prime numbers (also known as prime numbers), which are called the prime factors of this sum. For example, 6 can be decomposed into 2x3, and 24 can be decomposed into 2x2x3.

Now, your program reads an integer in the range of [2100000], and then outputs its prime factorization; when it reads a prime, it outputs itself.

 

Input format:

An integer in the range [2100000].

 

Output format:

Like:

n=axbxcxd

or

n=n

There is no space between all the symbols. x is the lowercase letter x.

 

Input example:

18

 

Output example:

18=2x3x3

 

Time limit: 500ms memory limit: 32000kb
 1 import java.util.Scanner;
 2 
 3 public class Main {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub        
 7         Scanner in = new Scanner(System.in);
 8         
 9         int n;//Input integer
10         int nCopy;//copy n Value
11         int prime=2;//Prime number, the first is 2
12         int count=0;//Number of output prime factors
13         
14         n=in.nextInt();
15         
16         if(prime(n)) 
17         {
18             System.out.printf("%d=%d\n",n,n);//n=n
19         }
20         else
21         {
22             System.out.printf("%d=",n);//n=?
23             
24             nCopy=n;
25             
26             while(nCopy>1)
27             {
28                 if(nCopy%prime==0)
29                 {
30                     if(count>0)
31                     {
32                         System.out.print("x");
33                     }
34                     nCopy=nCopy/prime;
35                     System.out.print(prime);
36                     count++;
37                 }
38                 else
39                 {
40                     prime++;//Skip current prime
41                     prime=nextPrime(prime);//Next prime
42                 }
43             }
44             
45             System.out.printf("\n");//Line feed
46             
47         }
48     }
49     
50     public static boolean prime(int n)//Determine whether prime number
51     {
52         boolean isPrime=true;//hypothesis n Prime number
53         if(n==1||(n%2==0&&n!=2))//Judge 1 and non-2 even numbers
54         {
55             isPrime=false;
56         }
57         else if(n==2)//Judge 2
58         {
59             isPrime=true;
60         }
61         else//Other judgement
62         {
63             for(int i=3;i<Math.sqrt(n);i=i+2)
64             {
65                 if(n%i==0)
66                 {
67                     isPrime=false;
68                     break;
69                 }
70             }
71         }
72         
73         return isPrime;
74     }
75     
76     public static int nextPrime(int n)//Find next prime
77     {
78         while(true)
79         {
80             if(prime(n))//If n Is prime, return n
81             {
82                 return n;
83             }
84             else//If not, n Self increment,Re judgement
85             {
86                 n++;
87             }
88         }
89     }
90 }

 

Posted by Awesome Trinity on Mon, 02 Dec 2019 10:42:40 -0800