Все задачи
Легко Решено: 0 Процент решений: 0%

Sum of Proper Divisors

Add up every divisor of n that is smaller than n.

Чему вы научитесь

  • Find divisors in pairs
  • Stop the loop at the square root
  • Avoid counting a square root divisor twice

Условие

Показать перевод

The proper divisors of n are the positive divisors other than n itself. For 12 they are 1, 2, 3, 4 and 6. Their sum (sometimes called the aliquot sum) tells whether a number is perfect, abundant or deficient. Divisors come in pairs d and n / d, so you only need to try values of d up to the square root of n.

Implement sum_of_proper_divisors(n).

Input

n: A positive integer.

Output

Return the sum of all positive divisors of n that are less than n. For n = 1 there are none, so return 0.

Ограничения

1 ≤ n ≤ 10^12

Примеры

Пример 1
Ввод:
n = 12

Ожидаемый вывод:
expected = 16
Ввод:
long long n = 12;

Ожидаемый вывод:
long long expected = 16;
Ввод:
long n = 12;

Ожидаемый вывод:
long expected = 16;

Пояснение

1 + 2 + 3 + 4 + 6 = 16.

Пример 2
Ввод:
n = 28

Ожидаемый вывод:
expected = 28
Ввод:
long long n = 28;

Ожидаемый вывод:
long long expected = 28;
Ввод:
long n = 28;

Ожидаемый вывод:
long expected = 28;

Пояснение

1 + 2 + 4 + 7 + 14 = 28, so 28 is a perfect number.

Как подступиться

Сначала попробуйте сами, затем открывайте подсказки по одной.

Подсказка 1

If d divides n, then n / d divides n too.

Подсказка 2

Only try d up to the square root of n, adding both members of each pair.

Подсказка 3

Leave n itself out, and add the square root only once when n is a perfect square.

Ожидаемая сложность

O(sqrt n) time, O(1) extra space

Ссылки

Это задание адаптировано для CodeUtility. Следуйте условию и ограничениям выше.

Лучшее принятое решение каждого участника. Более быстрые, экономные и высоко оценённые решения стоят выше.

Принятых решений пока нет. Решите задачу и займите первое место.

Учитесь у других

Решения сообщества

Пока нет опубликованных решений. Решите задание, чтобы поделиться первым.