nums의 각 원소는 1이상 1000이하의 자연수 이고, 중복된 숫자가 들어있지 않으므로
3개의 수를 더했을 때 최대로 나올 수 있는 수는 2997이다.
result의 인덱스의 값이 소수일때 True
,
소수가 아닐 경우 False
소수인 숫자를 먼저 구한다음
combination
로 입력된 숫자로 나올 수 있는 모든 조합을 구한다.
그 조합들을 다시 sum을 하여 list를 구한뒤
result[list의 값]이 true이면 소수 이므로
count를 +1해준다.
from itertools import combinations
result = [True] * 2998
result[0] = False
result[1] = False
for i in range(2, 1500):
if result[i]:
a = 2
while i * a <= 2997:
result[i*a] = False
a += 1
def solution(nums):
sosu = list(combinations(nums, 3))
sosu = list((map(sum, sosu)))
count = 0
for x in sosu:
if result[x]:
count += 1
return count
combinations
로 조합을 구한뒤 소수인지 아닌지를 판별
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