-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprimalrepresentation.java
46 lines (42 loc) · 1.39 KB
/
primalrepresentation.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.io.*;
import java.util.*;
public class primalrepresentation {
private static ArrayList<Integer> factors;
public static void main(String args[]) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
String line = "";
while((line = in.readLine()) != null) {
factors = new ArrayList<Integer>();
int n = Integer.parseInt(line);
factor(Math.abs(n));
if(n < 0)
out.print("-1 ");
while(!factors.isEmpty()) {
int factor = factors.get(0);
int freq = Collections.frequency(factors, factor);
if(freq > 1)
out.print(factor + "^" + freq + " ");
else
out.print(factor + " ");
factors.removeAll(Collections.singleton(factor));
}
out.println();
}
out.close();
}
private static void factor(int n) {
while(n % 2 == 0) {
factors.add(2);
n /= 2;
}
for(int i = 3; i <= Math.sqrt(n); i += 2) {
while(n % i == 0) {
factors.add(i);
n /= i;
}
}
if(n > 2)
factors.add(n);
}
}