-
[2022 KAKAO BLIND RECRUITMENT] k진수에서 소수 개수 구하기코딩 테스트/Level 2 2022. 1. 17. 09:56반응형
https://programmers.co.kr/learn/courses/30/lessons/92335
파이썬
def is_prime(num: int) -> bool: for i in range(2, int((num + 1) ** 0.5) + 1): if num % i == 0: return False return True def change_notation(n: int, k: int) -> str: new_num = '' while n > 0: m = n % k new_num += str(m) n = n // k return new_num[::-1] def solution(n, k): new_num = change_notation(n, k) new_nums = new_num.split('0') answer = 0 for each in new_nums: if each == '': continue each = int(each) if each > 1 and is_prime(each): answer += 1 return answer
자바
class Solution { boolean is_prime(long n) { if (n < 2) return false; for (var i = 2; i < (long) Math.sqrt(n + 1) + 1; i++) { if (n % i == 0) return false; } return true; } String change_notation(int n, int k) { int m; var new_num = new StringBuilder(); while (n > 0) { m = n % k; new_num.append(m); n = n / k; } return new_num.reverse().toString(); } public int solution(int n, int k) { int answer = 0; String[] nums = change_notation(n, k).split("0+"); for (String num : nums) if (is_prime(Long.parseLong(num))) answer++; return answer; } }
반응형