easy +10 pts

Sum of divisors

Compute the sum of all positive divisors of a given integer efficiently.

Write a function `sum_of_divisors(n: int) -> int` that returns the sum of all positive divisors of the integer `n`. For example, the divisors of 6 are 1, 2, 3, and 6, so the sum is 12. Your solution should handle `n` up to 10^6 efficiently; a simple loop up to `n` is acceptable but try to optimize by iterating only up to the square root.

Constraints

0 <= n <= 10^6

Example

>>> sum_of_divisors(1)
1
>>> sum_of_divisors(6)
12
>>> sum_of_divisors(12)
28
>>> sum_of_divisors(0)
0
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Divisors come in pairs: if d divides n, then n//d also divides n.
Be careful with perfect squares: the square root should only be counted once.
For n = 0, return 0.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.