Tous les défis
Difficile 0 résolus 0 % de réussite

Non-Abundant Sums

Add up the numbers up to a limit that cannot be written as the sum of two abundant numbers.

Ce que vous allez apprendre

  • Compute divisor sums for a range with a sieve
  • Mark reachable sums with a boolean array
  • Cut a double loop short once sums pass the limit

Problème

Afficher la traduction

A number is abundant when its proper divisors (all divisors except itself) add up to more than it. 12 is the smallest: 1 + 2 + 3 + 4 + 6 = 16. Some numbers can be written as the sum of two abundant numbers (the same one may be used twice, as in 24 = 12 + 12) and some cannot. Add up every positive integer from 1 to limit that cannot.

Implement non_abundant_sums(limit).

Find the divisor sums for every number at once with a sieve, list the abundant numbers, then mark every pair sum that stays within the limit.

Input

limit: The largest number to check.

Output

Return the sum of all n with 1 ≤ n ≤ limit that are not the sum of two abundant numbers.

Contraintes

1 ≤ limit ≤ 10,000

Exemples

Exemple 1
Entrée :
limit = 23

Sortie attendue :
expected = 276
Entrée :
long long limit = 23;

Sortie attendue :
long long expected = 276;
Entrée :
long limit = 23;

Sortie attendue :
long expected = 276;

Explication

No two abundant numbers add up to 23 or less, so every number counts: 1 + 2 + ... + 23 = 276.

Exemple 2
Entrée :
limit = 30

Sortie attendue :
expected = 411
Entrée :
long long limit = 30;

Sortie attendue :
long long expected = 411;
Entrée :
long limit = 30;

Sortie attendue :
long expected = 411;

Explication

24 = 12 + 12 and 30 = 12 + 18 are left out, so the answer is 465 − 24 − 30 = 411.

Comment l'aborder

Essayez d'abord seul, puis ouvrez les indices un par un.

Indice 1

For each i, add i to the divisor sum of 2i, 3i, 4i, ...

Indice 2

Collect the abundant numbers in increasing order.

Indice 3

For each pair a ≤ b, stop the inner loop once a + b passes the limit.

Complexité attendue

O(limit log limit + A²) time for A abundant numbers, O(limit) extra space

Références

Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.

La meilleure solution validée de chaque codeur. Les plus rapides, les plus légères et les mieux notées passent devant.

Aucune solution validée pour l'instant. Résolvez-le et prenez la première place.

Apprenez des autres

Solutions de la communauté

Aucune solution publiée pour l'instant. Résolvez ce défi pour partager la première.