- Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathFindPrimeNumTest.java
More file actions
Latest commit
59 lines (49 loc) · 1.42 KB
/
Copy pathFindPrimeNumTest.java
File metadata and controls
59 lines (49 loc) · 1.42 KB
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
packagealgorithm.basicMath;
importorg.junit.Test;
importstaticorg.hamcrest.CoreMatchers.is;
importstaticorg.junit.Assert.assertThat;
publicclassFindPrimeNumTest {
/*
TASK
주어지는 수 이하의 소수 개수를 구한다.
*/
@Test
publicvoidtest() {
assertThat(solution(-3), is(-1));
assertThat(solution(0), is(0));
assertThat(solution(1), is(0));
assertThat(solution(2), is(1));
assertThat(solution(3), is(2));
assertThat(solution(8), is(4));
assertThat(solution(12), is(5));
assertThat(solution(44), is(14));
//2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43
}
publicintsolution(intnum) {
if (num < 0) {
return -1;
}
int[] checkList = newint[num + 1];
for (inti = 2; i <= num; i++) {
checkList[i] = i;
}
intbaseNum = (int) Math.sqrt(num);
for (inti = 2; i <= baseNum; i++) {
if (checkList[i] == 0) {
continue;
}
for (intk = i; k <= num; k += i) {
if (checkList[k] != i && checkList[k] % i == 0) {
checkList[k] = 0;
}
}
}
intcount = 0;
for (inti = 0; i <= num; i++) {
if (checkList[i] != 0) {
count++;
}
}
returncount;
}
}