프로그래머스 - LV.1 - 소수 만들기

박종일·2023년 8월 25일
0

프로그래머스 LV.1

목록 보기
25/26

나의 풀이

import math
import numpy as np 

def is_prime_number(a, b, c):
    total = a + b + c
    for i in range(2, total // 2 + 1):
        if total % i == 0: return False
    return True


def solution(nums):
    answer = 0
    for i in range(0, len(nums) - 2):
        for j in range(i + 1, len(nums) - 1):
            for k in range(j + 1, len(nums)):
                if is_prime_number(nums[i], nums[j], nums[k]): 
                    answer += 1
    return answer
    

소수 판별 알고리즘

def is_prime_number(a, b, c):
    total = a + b + c
    for i in range(2, total // 2 + 1):
        if total % i == 0: return False
    return True

소수 판별 알고리즘에 대해서 알아둘 필요가 있다.

i가 1을 제외하고 2부터 자신의 수의 반까지 나눠서 이것이 나눠지는 수가 있으면
이건 소수가 아니라고 판별! 나머지가 있으면 소수로 판별하는 것이다!

다른 풀이

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

조합 라이브러리를 불러서 사용하는 문제가 꽤나 많다!
이 라이브러리를 뿌셔보자

profile
존경하는 인물: 스토브리그 백승수 단장(남궁민)

0개의 댓글