𝘼𝙣𝙖𝙡𝙮𝙨𝙞𝙨/ᴀʟɢᴏʀɪᴛʜᴍ
itertools-combinations, math
콜라맛갈비
2023. 4. 28. 12:15
728x90
문제 설명
주어진 숫자 중 3개의 수를 더했을 때 소수가 되는 경우의 개수를 구하려고 합니다. 숫자들이 들어있는 배열 nums가 매개변수로 주어질 때, nums에 있는 숫자들 중 서로 다른 3개를 골라 더했을 때 소수가 되는 경우의 개수를 return 하도록 solution 함수를 완성해주세요.
from itertools import combinations
import math
def is_prime(n) :
for i in range(2, int(math.sqrt(n)+1)) :
if n%i == 0 : return False
return True
def solution(nums) :
comb = list(combinations(nums, 3))
answer = 0
for i in comb :
if is_prime(i[0] + i[1] + i[2]) : answer += 1
return answer
728x90