Home [1일 1문제] 3월 16일 문제풀이
Post
Cancel

[1일 1문제] 3월 16일 문제풀이

✏️백준 17427번 : 약수의 합2

문제

나의 풀이

- Swift

1
2
3
4
5
6
7
8
let n = Int(readLine()!)!
var temp: [Int] = []

for i in (1...n+1) {
    temp.append(i * (n / i))
}

print(temp.reduce(0, +))

- Python

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
import sys

input = sys.stdin.readline

n = int(input())
temp = [i * (n//i) for i in range(1, n+1)]
print(sum(temp))


## 시간초과
# n = int(input())
# answer = 0

# for i in range(1, n+1):
#     divisors = []
#     root = int(math.sqrt(i) + 1)

#     for j in range(1, root):
#         if i % j == 0:
#             divisors.append(j)
#             divisors.append(int(i / j))

#     answer += sum(set(divisors))

# print(answer)

수학적 규칙을 찾는 것이 중요했던 문제
약수를 하나하나 구해서 더한다면 시간초과가 발생한다!

1
1 2
1 3
1 2 4
1 5
1 2 3 6
1 7
1 2 4 8
1 3 9
1 2 5 10

1 : 10(개)
2 : 5
3 : 3
4 : 2
5 : 2
6 : 1
7 : 1
8 : 1
9 : 1
10 : 1

This post is licensed under CC BY 4.0 by the author.