๐Ÿ”Ž ์†Œ์ˆ˜ ์ฐพ๊ธฐ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค Level2

[Class2] ์†Œ์ˆ˜ ์ฐพ๊ธฐ

๋ฌธ์ œ ์„ค๋ช…


ํ•œ์ž๋ฆฌ ์ˆซ์ž๊ฐ€ ์ ํžŒ ์ข…์ด ์กฐ๊ฐ์ด ํฉ์–ด์ ธ์žˆ์Šต๋‹ˆ๋‹ค. ํฉ์–ด์ง„ ์ข…์ด ์กฐ๊ฐ์„ ๋ถ™์—ฌ ์†Œ์ˆ˜๋ฅผ ๋ช‡ ๊ฐœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š”์ง€ ์•Œ์•„๋‚ด๋ ค ํ•ฉ๋‹ˆ๋‹ค.

๊ฐ ์ข…์ด ์กฐ๊ฐ์— ์ ํžŒ ์ˆซ์ž๊ฐ€ ์ ํžŒ ๋ฌธ์ž์—ด numbers๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ข…์ด ์กฐ๊ฐ์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ์†Œ์ˆ˜๊ฐ€ ๋ช‡ ๊ฐœ์ธ์ง€ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ


  • numbers๋Š” ๊ธธ์ด 1 ์ด์ƒ 7 ์ดํ•˜์ธ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.
  • numbers๋Š” 0~9๊นŒ์ง€ ์ˆซ์ž๋งŒ์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
  • โ€œ013โ€์€ 0, 1, 3 ์ˆซ์ž๊ฐ€ ์ ํžŒ ์ข…์ด ์กฐ๊ฐ์ด ํฉ์–ด์ ธ์žˆ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ


|numbers|return| |โ€”โ€”|โ€”โ€”| |โ€17โ€|3| |โ€011โ€|2|

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

์˜ˆ์ œ #1
[1, 7]์œผ๋กœ๋Š” ์†Œ์ˆ˜ [7, 17, 71]๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ์ œ #2
[0, 1, 1]์œผ๋กœ๋Š” ์†Œ์ˆ˜ [11, 101]๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

- 11๊ณผ 011์€ ๊ฐ™์€ ์ˆซ์ž๋กœ ์ทจ๊ธ‰ํ•ฉ๋‹ˆ๋‹ค.

# ๐Ÿ“– ๊ฐ€์ ธ๋‹ค ์“ฐ๊ธฐ

๋ฌธ์ œ ๋ถ„๋ฅ˜์— "์™„์ „ํƒ์ƒ‰"์ด๋ผ๊ณ  ์“ฐ์—ฌ ์žˆ์–ด์„œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด๋ฏธ ์•Œ๊ณ  ๋“ค์–ด๊ฐ”๋‹ค. 

์ถ”๊ฐ€์ ์ธ ์กฐ๊ฑด์ด๋ผ๋ฉด ์†Œ์ˆ˜๋ฅผ ์ฐพ๋Š” ๋ฌธ์ œ์ด๊ธฐ์— **์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด** ๋ฅผ ์“ฐ๋ฉด ๋˜๊ฒ ๋‹ค๊ณ  ์ƒ๊ฐํ•˜์˜€๋‹ค.

# ๐Ÿ“ ๊ณผ์ • ์„ค๊ณ„/๊ด€๋ฆฌ

![แ„‹แ…งแ†ซแ„‰แ…ณแ†ธแ„Œแ…กแ†ผ-59](https://user-images.githubusercontent.com/88064555/181423333-e85ac4fc-45ad-4835-ba71-19b5dc3a1d52.jpg)

๋กœ์ง์„ ์งœ๋‹ค๋ณด๋‹ˆ ์ˆœ์—ด์ด ํ•„์š”ํ•˜์˜€๋‹ค. ํŒŒ์ด์ฌ ๋‚ด์žฅ ๋ชจ๋“ˆ์ธ permutation์„ importํ•˜์—ฌ ์‚ฌ์šฉํ•˜์˜€๋‹ค.

# ๐Ÿ‘จ๐Ÿปโ€๐Ÿ’ป CODE

```python
from itertools import permutations as p

MAX = 9999999

# ์†Œ์ˆ˜ ๊ฒ€์ฆ ํ•จ์ˆ˜ - ์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด
def is_prime(n):
    sieve = [True]*(n+1)

    m = int(n**0.5)

    for i in range(2,m+1):
        if sieve[i] == True:
            for j in range(2*i,n+1,i):
                sieve[j] = False

    return sieve

prime = is_prime(MAX)
prime[0], prime[1] = False,False

def solution(numbers):
    arr = list(map(int,numbers))
    permu = []
    for i in range(1,len(arr)+1):
        permu.extend(list(p(arr,i)))
    result = []
    for i in range(len(permu)):
        temp = ""
        for j in range(len(permu[i])):
            temp += str(permu[i][j])
        result.append(int(temp))
    result = set(result)
    result = list(result)

    count = 0
    for i in range(len(result)):
        if prime[result[i]] == True:
            count += 1

    return count

๊ณ ์ˆ˜์˜ ํ’€์ด

์ด ๋ถ„์€ ์ง„์ •ํ•œ ํŒŒ์ดํ† ๋‹‰์ธ๋“ฏ โ€ฆ

from itertools import permutations
def solution(n):
    a = set()
    for i in range(len(n)):
        a |= set(map(int, map("".join, permutations(list(n), i + 1))))
    a -= set(range(0, 2))
    for i in range(2, int(max(a) ** 0.5) + 1):
        a -= set(range(i * 2, max(a) + 1, i))
    return len(a)

ยฉ 2022. All rights reserved.

Powered by Hydejack v9.1.6