Engineering platform~18 min2s · 256 MBtarget O(n log n) time, O(n) spacenumber-theory
You work on a storage service that splits a file of k blocks into equal chunks. A chunk size is valid when it divides k exactly, so no block is left over. The size k itself is valid: it is one chunk.
Your team lead asks: "For every file size from 1 to limit, what is the sum of its valid chunk sizes?" The capacity planner needs the answers for sizes up to 200,000.
Write divisor_sums(limit).
limit is the largest file size, in blocks.k is a whole number from 1 to k that divides k exactly.limit numbers. The value at position k - 1 is the sum of all valid chunk sizes of k.Input: limit = 6
Output: [1, 3, 4, 7, 6, 12]| k | Valid chunk sizes | Sum |
|---|---|---|
| 1 | 1 | 1 |
| 2 | 1, 2 | 3 |
| 3 | 1, 3 | 4 |
| 4 | 1, 2, 4 | 7 |
| 5 | 1, 5 | 6 |
| 6 | 1, 2, 3, 6 | 12 |
Input: limit = 1
Output: [1]A file of one block has one valid chunk size, 1.
Input: limit = 12
Output: [1, 3, 4, 7, 6, 12, 8, 15, 13, 18, 12, 28]For example, 12 has the chunk sizes 1, 2, 3, 4, 6 and 12, which add up to 28.
1 ≤ limit ≤ 200,000Finding the divisors of each number by trial division up to its square root takes about 89,400,000 steps for all 200,000 numbers. Testing every possible chunk size for every size takes about 20 billion. A method that touches each multiple of each number once takes about 2,472,113 steps.