코딩테스트/programmers
[월간 코드]약수의 개수와 덧셈
EunGyeongKim
2022. 9. 21. 17:19
더 많은 코드 (https://github.com/EunGyeongKim/TIL)
GitHub - EunGyeongKim/TIL: Today I Learne
Today I Learne. Contribute to EunGyeongKim/TIL development by creating an account on GitHub.
github.com
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
1. 문제 설명
두 정수 left
와 right
가 매개변수로 주어집니다. left
부터 right
까지의 모든 수들 중에서, 약수의 개수가 짝수인 수는 더하고, 약수의 개수가 홀수인 수는 뺀 수를 return 하도록 solution 함수를 완성해주세요.
2. 제한사항
• 1 ≤ left
≤ right
≤ 1,000
3. 입출력 예
left | right | result |
---|---|---|
13 | 17 | 43 |
24 | 27 | 52 |
4. 코드
import math
def solution(left, right):
answer = 0
for i in range(left, right+1):
if math.sqrt(i) == int(math.sqrt(i)) :
answer = answer - i
else:
answer = answer+ i
return answer