forked from improper4/uva
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UVa00160_FactorsandFactorials.java
77 lines (69 loc) · 1.67 KB
/
UVa00160_FactorsandFactorials.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package uva;
/* USER: 46724 (sfmunera) */
/* PROBLEM: 96 (160 - Factors and Factorials) */
/* SUBMISSION: 08927530 */
/* SUBMISSION TIME: 2011-06-07 20:23:22 */
/* LANGUAGE: 2 */
import java.util.*;
public class UVa00160_FactorsandFactorials {
static List<Integer> sieve(int n) {
int p = 2;
boolean[] P = new boolean[n];
Arrays.fill(P, true);
P[0] = P[1] = false;
while (p * p <= n) {
for (int i = p + p; i < n; i += p)
P[i] = false;
for (int i = p + 1; i < n; ++i)
if (P[i]) {
p = i;
break;
}
}
List<Integer> primes = new ArrayList<Integer>();
for (int i = 0; i < n; ++i)
if (P[i])
primes.add(i);
return primes;
}
public static void main(String[] args) {
try {
Scanner in = new Scanner(System.in);
List<Integer> primes = sieve(100);
while (true) {
int N = in.nextInt();
if (N == 0)
break;
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 2; i <= N; ++i) {
int tmp = i;
for (int j = 0; j < primes.size() && tmp != 1; ++j) {
int cnt = 0;
int p = primes.get(j);
while (tmp % p == 0) {
tmp /= p;
++cnt;
}
if (map.containsKey(p))
map.put(p, map.get(p) + cnt);
else
map.put(p, cnt);
}
}
System.out.printf("%3d! =", N);
int cnt = 0;
for (int x : primes) {
if (map.containsKey(x)) {
if (cnt > 0 && cnt % 15 == 0)
System.out.printf("\n ");
System.out.printf("%3d", map.get(x));
++cnt;
}
}
System.out.println();
}
} catch (Exception e) {
System.exit(0);
}
}
}