Notice
Recent Posts
Recent Comments
Link
«   2025/11   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30
Archives
Today
Total
관리 메뉴

DANIELOGIC

[Python] 백준 1977번 완전제곱수 본문

Algorithm/백준

[Python] 백준 1977번 완전제곱수

daniel; 2024. 6. 23. 22:01

[Python] 백준 1977번 완전제곱수

https://www.acmicpc.net/problem/1977

문제

코드

m = int(input())
n = int(input())
i = 1
l = []
while True:
    if i*i >= m and i*i <= n:
        l.append(i*i)
    elif i*i > n:
        break
    i += 1
if len(l) == 0:
    print(-1)
else:
    print(sum(l))
    print(min(l))

리뷰

for문 사용해서 푸는게 비효율적이라고 느꼈다.

그냥 무한루프 돌린 다음에 i*i이 n을 넘어가는 순간 바로 탈출 시키면

훨씬 효율적으로 작동할 것 같다.

다른 풀이

m = int(input())
n = int(input())
x = 1
l = []
while x**2 <= n:
    if x**2 >= m:
        l.append(x**2)
    x += 1
if len(l) > 0:
    print(sum(l))
    print(min(l))
else:
    print(-1)

'Algorithm > 백준' 카테고리의 다른 글

[Python] 백준 1408번 24  (0) 2024.06.23
[Python] 백준 5635번 생일  (0) 2024.06.23
[Python] 백준 11557번 Yangjojang of The Year  (0) 2024.06.23
[Python] 백준 7567번 그릇  (0) 2024.06.23
[Python] 백준 2884번 알람 시계  (0) 2024.06.23