# ★★★☆☆ k진수에서 소수 개수 구하기
# 구현코드
public class Solution {
public int solution(int n, int k) {
int answer = 0;
String value = makeJinSu(n, k,"");
String[] values = value.split("0+");
for (int i = 0; i < values.length; i++) {
if (values[i].equals("1"))
continue;
if (isPrime(Long.parseLong(values[i])))
answer++;
}
return answer;
}
private boolean isPrime(long val) {
if(val == 1L)
return false;
for (long j = 2; j * j <= val; j++) {
if (val % j == 0) {
return false;
}
}
return true;
}
public String makeJinSu(int n, int k,String result) {
if(n < k)
return n%k+result;
return makeJinSu(n/k,k,n%k + result);
}
}```
---
<Comment />
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
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