programmers.co.kr/learn/courses/30/lessons/12977
코딩테스트 연습 - 소수 만들기
주어진 숫자 중 3개의 수를 더했을 때 소수가 되는 경우의 개수를 구하려고 합니다. 숫자들이 들어있는 배열 nums가 매개변수로 주어질 때, nums에 있는 숫자들 중 서로 다른 3개를 골라 더했을 때
programmers.co.kr
from itertools import combinations
def is_prime(nums_case):
candidate = sum(nums_case)
for i in range(2, int(candidate**0.5)+1):
if candidate % i == 0:
return False
return True
def solution(nums):
count = 0
for nums_case in combinations(nums, 3):
if is_prime(nums_case):
count += 1
return count
조합과 소수 판별 알고리즘이 합쳐진 문제이다.
itertools 를 사용할 수 있고 소수 판별 알고리즘을 알고 있다면 쉽게 풀 수 있다.
'PS > 프로그래머스' 카테고리의 다른 글
[프로그래머스-Level 2] 스킬트리_파이썬 (0) | 2021.04.09 |
---|---|
[프로그래머스-Level 1] 실패율_파이썬 (0) | 2021.04.08 |
[프로그래머스-Level 1] 최대공약수와 최소공배수 ❌⭕ (0) | 2021.04.08 |
[프로그래머스-Level 1] 키패드 누르기_파이썬 ❌⭕ (0) | 2021.04.07 |
[프로그래머스-Level 1] 이상한 문자 만들기_파이썬 (0) | 2021.04.06 |