๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm/๋ฐฑ์ค€(BOJ)

[ ํŒŒ์ด์ฌ(python) ] ๋ฐฑ์ค€ 1977 - ์™„์ „ ์ œ๊ณฑ ์ˆ˜

by YWTechIT 2021. 5. 17.
728x90

๐Ÿ“ ๋ฐฑ์ค€ 1977 - ์™„์ „ ์ œ๊ณฑ์ˆ˜

๋ฐฑ์ค€ 1977 - ์™„์ „ ์ œ๊ณฑ์ˆ˜


โšก๏ธ ๋‚˜์˜ ํ’€์ด

์™„์ „ ์ œ๊ณฑ์ˆ˜๋ฅผ ๋งŒ๋“ค ๋•Œ๋Š” root๋งŒ ๊ณ„์‚ฐํ•˜๋ฉด ๋˜๋Š” ์ค„ ์•Œ์•˜๋Š”๋ฐ, ๋ฐ˜๋ก€๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์‹œ ์ œ๊ณฑ์„ ์ทจํ•ด์ค˜์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์ฒ˜์Œ ์•Œ์•˜๋‹ค.

๊ทธ๋ฆฌ๊ณ  root๋ฅผ ์ทจํ•  ๋•Œ math.sqrt ๋Œ€์‹  i ** 0.5๋ฅผ ํ•˜๋ฉด ๊ฐ„ํŽธํ•˜๊ฒŒ ๊ณ„์‚ฐ ํ•  ์ˆ˜ ์žˆ๋‹ค. (์ด๊ฒƒ๋„ ์ฒ˜์Œ ์•Œ์•˜๋‹ค.)

  1. ์ฒ˜์Œ ์ˆ˜๋ฅผ root๋ฅผ ์ทจํ•˜์ž(i^0.5) (first = int(i \*\* 0.5))
  2. first์— ๋‹ค์‹œ ๊ฑฐ๋“ญ์ œ๊ณฑ(i^2)์„ ํ•ด์ค€๋‹ค. (second = first \*\* 2)

while๋ฌธ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค. ๋งŒ์•ฝ, append๊ฐ’์ด ์—†์œผ๋ฉด ( num == [] )๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•œ๋‹ค.

m = int(input())
n = int(input())

num = []
for i in range(m, n+1):
    root = int(i ** 0.5)
    if i == root ** 2:num.append(i)

if num == []:
    print(-1)
else:
    print(f'{sum(num)}\n{min(num)}')

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€