아이공의 AI 공부 도전기

[Baekjoon] 1977번 : 완전제곱수 (Python, 브루트포스)

 

     

 

 

 

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

 

1977번: 완전제곱수

M과 N이 주어질 때 M이상 N이하의 자연수 중 완전제곱수인 것을 모두 골라 그 합을 구하고 그 중 최솟값을 찾는 프로그램을 작성하시오. 예를 들어 M=60, N=100인 경우 60이상 100이하의 자연수 중 완

www.acmicpc.net

코드 링크

https://github.com/stellaluminary/Baekjoon

 

GitHub - stellaluminary/Baekjoon

Contribute to stellaluminary/Baekjoon development by creating an account on GitHub.

github.com

 

Python

 

방법 1 - 메모리 30840KB / 시간 68ms / 코드 길이 B

 

n = int(input())
m = int(input())
t = [i**2 for i in range(1,101)]
s = []
for i in t:
    if n <= i <= m:
        s.append(i)
if len(s):
    print(sum(s))
    print(min(s))
else:
    print(-1)

 

방법 2 - 메모리 30840KB / 시간 68ms / 코드 길이 B

 

n = int(input())
m = int(input())
t = [i**2 for i in range(101) if n <= i**2 <= m]
print(f'{sum(t)}\n{t[0]}'if t else -1)

 

공유하기

facebook twitter kakaoTalk kakaostory naver band
loading