[๋ฐฑ์ค€/python] 4948๋ฒˆ : ๋ฒ ๋ฅดํŠธ๋ž‘ ๊ณต์ค€

2021. 3. 16. 23:45

๋ฌธ์ œ

๋ฒ ๋ฅดํŠธ๋ž‘ ๊ณต์ค€์€ ์ž„์˜์˜ ์ž์—ฐ์ˆ˜ n์— ๋Œ€ํ•˜์—ฌ, n๋ณด๋‹ค ํฌ๊ณ , 2n๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์†Œ์ˆ˜๋Š” ์ ์–ด๋„ ํ•˜๋‚˜ ์กด์žฌํ•œ๋‹ค๋Š” ๋‚ด์šฉ์„ ๋‹ด๊ณ  ์žˆ๋‹ค.

์ด ๋ช…์ œ๋Š” ์กฐ์ œํ”„ ๋ฒ ๋ฅดํŠธ๋ž‘์ด 1845๋…„์— ์ถ”์ธกํ–ˆ๊ณ , ํŒŒํ”„๋ˆ„ํ‹ฐ ์ฒด๋น„์‡ผํ”„๊ฐ€ 1850๋…„์— ์ฆ๋ช…ํ–ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, 10๋ณด๋‹ค ํฌ๊ณ , 20๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์†Œ์ˆ˜๋Š” 4๊ฐœ๊ฐ€ ์žˆ๋‹ค. (11, 13, 17, 19) ๋˜, 14๋ณด๋‹ค ํฌ๊ณ , 28๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์†Œ์ˆ˜๋Š” 3๊ฐœ๊ฐ€ ์žˆ๋‹ค. (17,19, 23)

์ž์—ฐ์ˆ˜ n์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, n๋ณด๋‹ค ํฌ๊ณ , 2n๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์†Œ์ˆ˜์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. 

 

์ž…๋ ฅ

์ž…๋ ฅ์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค. ๊ฐ ์ผ€์ด์Šค๋Š” n์„ ํฌํ•จํ•˜๋Š” ํ•œ ์ค„๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค.

์ž…๋ ฅ์˜ ๋งˆ์ง€๋ง‰์—๋Š” 0์ด ์ฃผ์–ด์ง„๋‹ค.

 

์ถœ๋ ฅ

๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์— ๋Œ€ํ•ด์„œ, n๋ณด๋‹ค ํฌ๊ณ , 2n๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์†Œ์ˆ˜์˜ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 

์ •๋‹ต

case = []
case.append(int(input()))
while case[-1] != 0:
    case.append(int(input()))
del case[-1]

numbers = [True for i in range((max(case) * 2)+1)]
numbers[0] = False
numbers[1] = False

for i in range(2, int(len(numbers)**0.5+1)):
    if numbers[i]:
        for n in range(i**2, len(numbers), i):
            numbers[n] = False

for n in case:
    result = 0
    for t in range(n+1, (n * 2)+1):
        if numbers[t] == True:
            result += 1
    print(result)

n+1๋ถ€ํ„ฐ 2n๊นŒ์ง€์˜ ์†Œ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ๋กœ, ์ด์ „์— ํ’€์—ˆ๋˜ ์†Œ์ˆ˜๊ตฌํ•˜๊ธฐ ๋ฌธ์ œ์™€ ์œ ์‚ฌํ•˜๋‹ค.

2021.03.09 - [๐Ÿ”์•Œ๊ณ ๋ฆฌ์ฆ˜/๋ฐฑ์ค€] - [๋ฐฑ์ค€/python] 1929๋ฒˆ : ์†Œ์ˆ˜ ๊ตฌํ•˜๊ธฐ

 

[๋ฐฑ์ค€/python] 1929๋ฒˆ : ์†Œ์ˆ˜ ๊ตฌํ•˜๊ธฐ

๋ฌธ์ œ M์ด์ƒ N์ดํ•˜์˜ ์†Œ์ˆ˜๋ฅผ ๋ชจ๋‘ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ์ž…๋ ฅ ์ฒซ์งธ ์ค„์— ์ž์—ฐ์ˆ˜ M๊ณผ N์ด ๋นˆ ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ฃผ์–ด์ง„๋‹ค. (1 ≤ M ≤ N ≤ 1,000,000) M์ด์ƒ N์ดํ•˜์˜ ์†Œ์ˆ˜๊ฐ€ ํ•˜๋‚˜ ์ด์ƒ ์žˆ๋Š” ์ž…

ye333.tistory.com

case = []
case.append(int(input()))
while case[-1] != 0:
    case.append(int(input()))
del case[-1]

์ž…๋ ฅ์„ ๋ฐ›์€ ํ›„ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ฅผ case์— ์ €์žฅํ•œ๋‹ค.

๋งˆ์ง€๋ง‰ ์ž…๋ ฅ์€ ์ž…๋ ฅ์„ ์ข…๋ฃŒํ•˜๋Š” ์˜๋ฏธ์˜ 0์ด๋ฏ€๋กœ ์‚ญ์ œํ•œ๋‹ค.

numbers = [True for i in range((max(case) * 2)+1)]
numbers[0] = False
numbers[1] = False

์ž…๋ ฅ๋ฐ›์€ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ์ตœ๋Œ€๊ฐ’์„ ๋‘๋ฐฐํ•œ ์ˆ˜๊ฐ€ ๊ฐ€์žฅ ํฐ ๊ฐ’์ด๋ฏ€๋กœ ๋ฒ”์œ„๋ฅผ ์œ„์™€๊ฐ™์ด ์žก์•„์ค€๋‹ค.

๐Ÿ’ฅ ์—ฌ๊ธฐ์„œ ์ฃผ์˜ํ•  ์ ์€ range์— ์ž…๋ ฅ๋œ ์ˆซ์ž์˜ -1๊ฐ’๊นŒ์ง€์˜ ์ธ๋ฑ์Šค๊ฐ€ ๋งŒ๋“ค์–ด์ง€๋ฏ€๋กœ +1ํ•ด์ฃผ์–ด์•ผํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

0๊ณผ 1์€ ์†Œ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ฏ€๋กœ ๋ฏธ๋ฆฌ False๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค. 

 

for i in range(2, int(len(numbers)**0.5+1)):
    if numbers[i]:
        for n in range(i**2, len(numbers), i):
            numbers[n] = False

์œ„ ์ฝ”๋“œ๋Š” ์†Œ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ•ต์‹ฌ ์ฝ”๋“œ๋กœ ์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด๋ฅผ ์ด์šฉํ•œ๋‹ค.

์ด์— ๋Œ€ํ•ด ์•„๋ž˜์˜ ๊ฒŒ์‹œ๋ฌผ์— ์ž์„ธํžˆ ์ ์–ด ๋†“์•˜๋‹ค.

2021.03.10 - [๐Ÿ”์•Œ๊ณ ๋ฆฌ์ฆ˜/๐Ÿ”…๊ฐœ๋…์ •๋ฆฌ] - [ํŒŒ์ด์ฌ/python] ์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด

 

[ํŒŒ์ด์ฌ/python] ์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด

์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด๋Š” ๊ณ ๋Œ€ ๊ทธ๋ฆฌ์Šค ์ˆ˜ํ•™์ž์ธ ์—๋ผํ† ์Šคํ…Œ๋„ค์Šค๊ฐ€ ๊ณ ์•ˆํ•ด ๋‚ธ ์†Œ์ˆ˜๋ฅผ ์ฐพ๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ๋งˆ์น˜ ์ฒด๋กœ ๊ฑธ๋Ÿฌ๋‚ด๋“ฏ์ด ์ˆ˜๋ฅผ ๊ฑธ๋Ÿฌ๋‚ธ๋‹ค๊ณ  ํ•˜์—ฌ ๋ถ™์—ฌ์ง„ ์ด๋ฆ„์ด๋‹ค. ์ž„์˜์˜ ์ž์—ฐ์ˆ˜ n์— ๋Œ€ํ•ด ์†Œ์ˆ˜

ye333.tistory.com

for n in case:
    result = 0
    for t in range(n+1, (n * 2)+1):
        if numbers[t] == True:
            result += 1
    print(result)

๋ชจ๋“  case์˜ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•ด ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•œ๋‹ค.

์†Œ์ˆ˜๊ฐ€ ๋ช‡๊ฐœ์ธ์ง€ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ์ด๋ฏ€๋กœ result๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ๊ณ 

๋ฒ”์œ„๊ฐ€ n+1๋ถ€ํ„ฐ 2n๊นŒ์ง€์ด๋ฏ€๋กœ range(n+1, (n*2)+1)๋กœ ์„ค์ •ํ•ด์ค€๋‹ค.

๐Ÿ’ฅ ์œ„์—์„œ ์–ธ๊ธ‰ํ•œ ์ฃผ์˜ํ•  ์ ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ range์— ์ž…๋ ฅ๋˜๋Š” ๋๊ฐ’์ด ํฌํ•จ๋˜๋ ค๋ฉด +1์„ ํ•ด์ฃผ์–ด์•ผํ•œ๋‹ค.

728x90

BELATED ARTICLES

more