https://programmers.co.kr/learn/courses/30/lessons/12977
코딩테스트 연습 - 소수 만들기
주어진 숫자 중 3개의 수를 더했을 때 소수가 되는 경우의 개수를 구하려고 합니다. 숫자들이 들어있는 배열 nums가 매개변수로 주어질 때, nums에 있는 숫자들 중 서로 다른 3개를 골라 더했을 때
programmers.co.kr
from itertools import combinations
import math
def is_prime_number(x):
for i in range(2, int(math.sqrt(x)) + 1):
if x % i == 0:
return False
return True
def solution(nums):
prime_candidate = list(combinations(nums, 3))
answer = 0
for i in prime_candidate:
if is_prime_number(sum(i)):
answer +=1
return answer
'Programming > Programmers' 카테고리의 다른 글
[프로그래머스] 위클리 챌린지 8주차 - 최소직사각형 (Python) (0) | 2021.09.28 |
---|---|
[프로그래머스] 폰캣몬 (Python) (0) | 2021.09.28 |
[프로그래머스] 음양 더하기(Python) (0) | 2021.09.27 |
[프로그래머스] 숫자 문자열과 영단어(Python) (0) | 2021.09.27 |
[프로그래머스] 등굣길(Python) (0) | 2021.08.12 |
댓글