-
[프로그래머스] 소수 만들기Engineering WIKI/Programmers 2022. 4. 1. 02:38
코딩테스트 연습 - 소수 만들기
주어진 숫자 중 3개의 수를 더했을 때 소수가 되는 경우의 개수를 구하려고 합니다. 숫자들이 들어있는 배열 nums가 매개변수로 주어질 때, nums에 있는 숫자들 중 서로 다른 3개를 골라 더했을 때
programmers.co.kr
내 풀이
import math from itertools import combinations def is_prime(sum_num): # 2부터 sum_num의 제곱근까지의 모든 수를 확인 for i in range(2, int(math.sqrt(sum_num)) + 1): # sum_num이 해당 수로 나뉘어지면 if sum_num % i == 0: return False return True def solution(nums): list_num = list(combinations(nums, 3)) prime_list = [] for i in list_num: if is_prime(sum(i)): prime_list.append(sum(i)) return len(prime_list)
다른 방법
def solution(nums): from itertools import combinations as cb answer = 0 for a in cb(nums, 3): cand = sum(a) for j in range(2, cand): if cand%j==0: break else: answer += 1 return answer
'Engineering WIKI > Programmers' 카테고리의 다른 글
[프로그래머스] 3진법 뒤집기 (0) 2022.04.05 [프로그래머스] 실패율 (0) 2022.04.02 [프로그래머스] 폰켓몬 (1) 2022.04.01 [프로그래머스] 모의고사 (0) 2022.04.01 [프로그래머스] 내적 (1) 2022.04.01 [프로그래머스] 음양 더하기 (0) 2022.04.01 [프로그래머스] 없는 숫자 더하기 (0) 2022.04.01 [프로그래머스] 크레인 인형뽑기 게임 (0) 2022.04.01