[Python/ํŒŒ์ด์ฌ] [๐Ÿฅˆ3] ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 4948 - ๋ฒ ๋ฅดํŠธ๋ž‘ ๊ณต์ค€

keyneneยท2022๋…„ 10์›” 19์ผ
0

Python

๋ชฉ๋ก ๋ณด๊ธฐ
12/26
post-custom-banner

๐Ÿ“–[Python/ํŒŒ์ด์ฌ][๐Ÿฅˆ3] ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 4948 - ๋ฒ ๋ฅดํŠธ๋ž‘ ๊ณต์ค€

๐Ÿ“œ๋ฌธ์ œ



๐Ÿ“•ํ’€์ด๋ฐฉํ–ฅ

๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„ ๋ฒ”์œ„(1 โ‰ค n โ‰ค 123,456)๋ฅผ ์ฃผ์˜ํ•˜์ž (2n๊นŒ์ง€์ด๋ฏ€๋กœ 246,912๊นŒ์ง€..)
๊ฐ€์žฅ ์ตœ์•…์˜ O(N)์„ ์ƒ๊ฐํ•ด๋ณด์ž (n=123,456์ด๊ณ  2n=246,912 ..)

๋‚ด ํฌ์ŠคํŒ… : [Python] ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 1929 - ์†Œ์ˆ˜ ๊ตฌํ•˜๊ธฐ
์ „์— ์†Œ์ˆ˜๊ตฌํ•˜๊ธฐ ๋ฌธ์ œ๋ฅผ ํ‘ผ ์ ์ด ์žˆ๋Š”๋ฐ, ์ด๋Ÿฐ์‹์œผ๋กœ ์ ‘๊ทผํ•˜๋ฉด ์ด๋ฒˆ์—๋Š” ์‹œ๊ฐ„์ดˆ๊ณผ๐Ÿคฆ๐Ÿปโ€โ™€๏ธ

์šฐ์„  2n์ด ๊ธฐ์ค€์ด๋ฏ€๋กœ 2~246,912๊นŒ์ง€ ์†Œ์ˆ˜๋ฅผ ๋ชจ๋‘ list(num)์— ์ €์žฅํ•˜์ž
๋ฌดํ•œ๋ฃจํ”„๋กœ n๊ฐ’์ด 0์•„๋‹ˆ๋ฉด num์— ์ ‘๊ทผํ•˜์—ฌ n~2n๊นŒ์ง€ ๊ฒ€์‚ฌํ•˜๋ฉด์„œ,
์†Œ์ˆ˜๊ฐ€ ์กด์žฌํ•˜๋ฉด res+1๋ฅผ ํ•˜์—ฌ res๋ฅผ ์ถœ๋ ฅํ•˜์ž


๐Ÿ“์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌํ˜„์ˆœ์„œ

  1. 2~246,912๊นŒ์ง€ ์†Œ์ˆ˜๋ฅผ list(num)์— ์ €์žฅํ•˜๊ธฐ
  2. ๋ฌดํ•œ๋ฃจํ”„๋กœ n==0์ด๋ฉด break, ์•„๋‹ˆ๋ฉด num์š”์†Œ๋ฅผ n~2n๊นŒ์ง€ ๊ฒ€์‚ฌํ•˜๋ฉด์„œ res++ ํ•ด์ฃผ๊ธฐ

๐Ÿ’ป๊ฒฐ๊ณผ์ฝ”๋“œ

num = []

for i in range(2, 246913):
    cnt = 0

    for p in range(2, int(i**0.5)+1):
        if i % p == 0:
            cnt += 1
            break

    if cnt == 0:
        num.append(i)

while True:
    n = int(input())
    res = 0

    if n == 0:
        break

    for i in num:
        if n < i <= 2*n:  # if i > n and i <=2*n๊ณผ ๊ฐ™์Œ
            res += 1

    print(res)

โœ๏ธ1. 2~246,912๊นŒ์ง€ ์†Œ์ˆ˜๋ฅผ list(num)์— ์ €์žฅํ•˜๊ธฐ

num = [] #๋นˆ๋ฆฌ์ŠคํŠธ

for i in range(2, 246913):
    cnt = 0

    for p in range(2, int(i**0.5)+1): 
        if i % p == 0: 
            cnt += 1
            break #cnt๊ฐ€ 0์ด ์•„๋‹ˆ๋ฉด ์†Œ์ˆ˜์•„๋‹˜

    if cnt == 0: #cnt๊ฐ€ 0์ด๋ฉด ์†Œ์ˆ˜์ด๋ฏ€๋กœ num์— ์ €์žฅ
        num.append(i)

โœ๏ธ2. ๋ฌดํ•œ๋ฃจํ”„๋กœ list(num)์˜ ๊ฐœ์ˆ˜ ์ถœ๋ ฅ

while True:
    n = int(input())
    res = 0

    if n == 0:  #0์ด๋ฉด ์ข…๋ฃŒ
        break

    for i in num: #num์š”์†Œ ํ•˜๋‚˜์”ฉ ๊ฒ€์‚ฌ
        if n < i <= 2*n: #i๊ฐ€ n๋ณด๋‹ค ํฌ๊ณ  2n๋ณด๋‹ค ์ž‘์€ ๊ฐ’์ผ๋•Œ๋งˆ๋‹ค res++
            res += 1

    print(res)

๐Ÿ“š์ดˆ๊ธฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์˜ค๋ฅ˜์™€ ์ •๋ฆฌ

โ€ป์ดˆ๊ธฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜

โœ๐Ÿป์†Œ์ˆ˜๊ตฌํ•˜๋Š” ํ•จ์ˆ˜ ๊ตฌํ˜„, n๊ฐ’ ์ž…๋ ฅ๋  ๋•Œ๋งˆ๋‹ค n~2n์†Œ์ˆ˜ ๊ฒ€์‚ฌ ํ›„ cnt++

import sys
input = sys.stdin.readline

def gj(n):
    for j in range(2,int((n**0.5))+1):
        if n%j == 0:
            return False
    return True

cnt = 0

while True:
    n = int(input().rstrip())
    cnt = 0
    if n == 0:
        break
    else:
        for i in range(n+1,(2*n)+1):
            if gj(i):
                cnt += 1
        print(cnt)
โ€ป๊ตฌํ˜„์ˆœ์„œ
  1. ์†Œ์ˆ˜๊ตฌํ•˜๋Š” gj()ํ•จ์ˆ˜ ๋งŒ๋“ค์–ด๋†“๊ธฐ
  2. n์ž…๋ ฅ๊ฐ’์—๋”ฐ๋ผ ํ•จ์ˆ˜๋ฅผ n~2n๊นŒ์ง€ ํ˜ธ์ถœํ•˜์—ฌ cnt++

๐Ÿ‘‰๐Ÿป n๊ฐ’์ด ๋“ค์–ด์˜ฌ ๋•Œ๋งˆ๋‹ค n~2n๊นŒ์ง€ ์†Œ์ˆ˜๋ฅผ ๊ณ„์†ํ•ด์„œ ๊ณ„์‚ฐํ•ด์•ผ ํ•˜๋ฏ€๋กœ ์‹œ๊ฐ„์ดˆ๊ณผ๐Ÿคฆ๐Ÿปโ€โ™€๏ธ


  1. ์ดˆ๊ธฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์„ค๊ณ„๊ฐ€ ๋งค์šฐ ์ค‘์š”ํ•˜๋‹ค
    ์ด ๋•Œ๊นŒ์ง€๋Š” ๋ชจ๋“  ์ผ€์ด์Šค๋ฅผ ๋ธŒ๋ฃจํŠธํฌ์Šค๋กœ ๊ณ„์‚ฐํ•˜๋‹ค๊ฐ€ ์˜ค๋ฅ˜๊ฐ€ ๋‚˜๊ธฐ ์ผ์‘ค์˜€๋Š”๋ฐ,
    ์œ„ ๋ฌธ์ œ๋Š”, ์ฃผ์–ด์ง„ ๋ชจ๋“  ์ผ€์ด์Šค๋ฅผ ์ €์žฅํ•ด๋‘๊ณ  ๊บผ๋‚ด์“ฐ๋Š” ๊ฒƒ์ด ๋” ๋น ๋ฅด๋‹ค....
    ๐Ÿ‘‰๐Ÿป ๊ตฌํ˜„ ์ „ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์„ค๊ณ„ ๊ณผ์ •์—์„œ ๊ณผ์—ฐ ์–ด๋–ค ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์ ํ•ฉํ•œ์ง€ ํŒ๋‹จํ•˜๋Š” ์—ฐ์Šตํ•˜์ž
profile
keynene
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€