Tutte le sfide
Difficile 0 risolte 0% di accettazione

Non-Abundant Sums

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

Cosa imparerai

  • 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

Problema

Mostra traduzione

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.

Vincoli

1 ≤ limit ≤ 10,000

Esempi

Esempio 1
Input:
limit = 23

Output atteso:
expected = 276
Input:
long long limit = 23;

Output atteso:
long long expected = 276;
Input:
long limit = 23;

Output atteso:
long expected = 276;

Spiegazione

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

Esempio 2
Input:
limit = 30

Output atteso:
expected = 411
Input:
long long limit = 30;

Output atteso:
long long expected = 411;
Input:
long limit = 30;

Output atteso:
long expected = 411;

Spiegazione

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

Come affrontarlo

Prova prima da solo, poi apri un suggerimento alla volta.

Suggerimento 1

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

Suggerimento 2

Collect the abundant numbers in increasing order.

Suggerimento 3

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

Complessità attesa

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

Riferimenti

Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.

La migliore soluzione superata di ogni programmatore. Le più veloci, leggere e meglio valutate stanno in alto.

Ancora nessuna soluzione superata. Risolvila e prendi il primo posto.

Impara dagli altri

Soluzioni della community

Nessuna soluzione pubblicata. Risolvi questa sfida per condividere la prima.