-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathFactorialZeroCount.java
54 lines (44 loc) · 1.14 KB
/
FactorialZeroCount.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
package exercise;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
public class FactorialZeroCount {
/*
TASK
n!의 결과값에서 0의 개수를 구한다.
*/
@Test
public void test() {
assertThat(countZero1(getFactorial(5)), is(1));
assertThat(countZero1(getFactorial(12)), is(2));
assertThat(countZero2(5), is(1));
assertThat(countZero2(12), is(2));
}
public int getFactorial(int num) {
int result = 1;
for (int i = 1; i <= num; i++) {
result *= i;
}
return result;
}
public int countZero1(int num) {
int count = 0;
while (num % 10 == 0) {
num /= 10;
count++;
}
return count;
}
// 5가 얼마나 곱해졌는지가 중요하다.
public int countZero2(int num) {
int count = 0;
for (int i = 5; i <= num; i += 5) {
int base = i;
while (base % 5 == 0) {
base /= 5;
count++;
}
}
return count;
}
}