알고리즘/카카오
[프로그래머스] k진수에서 소수 개수 구하기
BSHwan
2022. 8. 12. 23:55
반응형
https://school.programmers.co.kr/learn/courses/30/lessons/92335
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
public class Solution {
public static int solution(int n, int k) {
int answer = 0;
// 10진수 -> k 진수로 변환
String radix = Integer.toString(n, k);
// System.out.println(radix);
String[] strArr = radix.split("0");
// 0으로 split 한 배열에서 조건에 맞는지 검색.
for(int i=0; i<strArr.length; i++) {
String s = strArr[i];
if(s.equals("")) continue;
if(isPrime(Long.parseLong(s))) {
answer ++;
}
}
// System.out.println(answer);
return answer;
}
public static boolean isPrime(long n) {
if(n == 1) return false;
else if (n==2) return true;
for(int i=2; i<=Math.sqrt(n); i++) {
if(n % i == 0) {
return false;
}
}
return true;
}
}
반응형