거북이개발자

[프로그래머스] 소수 만들기 본문

Algorithm(Python)/programmers(Lv.1)

[프로그래머스] 소수 만들기

류정식 2021. 6. 23. 13:45

0. 제목

  • 프로그래머스 소수만들기

1. 문제

https://programmers.co.kr/learn/courses/30/lessons/12977

 

코딩테스트 연습 - 소수 만들기

주어진 숫자 중 3개의 수를 더했을 때 소수가 되는 경우의 개수를 구하려고 합니다. 숫자들이 들어있는 배열 nums가 매개변수로 주어질 때, nums에 있는 숫자들 중 서로 다른 3개를 골라 더했을 때

programmers.co.kr


2. 풀이

  • from itertools import combinations으로 combination을 사용해야한다.
  • 소수구분은 2부터 1씩올리는 반복문으로 나눠지는지 확인했다.

 


3. 코드

from itertools import combinations

def solution(nums):
    answer =0
    length=len(nums)
    three_sum=[]
    three_sum2=[]
    three_sum=list(combinations(nums, 3))

    for i in three_sum:
        flag=0
        for j in range(2, sum(i)):
            if sum(i)%j==0:
                flag=1
                break;
        if flag==0:
            answer+=1
            
    
    return answer
Comments