Search~25 min2s · 256 MBtarget O(top log log top + n log top) time, O(top) spacenumber-theory
You work on a search engine that keeps its index in shards. A shard holding v documents can be split in one round into p equal parts, where p is a prime number that divides v. Every part can be split again in the same way, until each part holds exactly one document.
The number of rounds does not depend on which primes you pick. Your team lead asks: "For each shard size, how many rounds does it take to reach single documents?" A request holds up to 100,000 shard sizes, each up to 1,000,000 documents.
Write split_rounds(sizes).
sizes[i] is the number of documents in shard i.A shard with 1 document needs 0 rounds. A shard whose size is prime needs 1 round.
Input: sizes = [12, 1, 97, 64, 360]
Output: [3, 0, 1, 6, 6]Each round divides the size by a prime. Split the largest prime off first or last: the count is the same.
| Size | A chain of rounds | Rounds |
|---|---|---|
| 12 | 12, 6, 3, 1 | 3 |
| 1 | 1 | 0 |
| 97 | 97, 1 | 1 |
| 64 | 64, 32, 16, 8, 4, 2, 1 | 6 |
| 360 | 360, 180, 90, 45, 15, 5, 1 | 6 |
Input: sizes = [1000000, 999983]
Output: [12, 1]1,000,000 is 2 to the power 6 times 5 to the power 6, which is 12 prime factors. 999,983 is itself a prime.
Input: sizes = [2, 3, 4]
Output: [1, 1, 2]Sizes 2 and 3 are primes. Size 4 splits into 2 parts and then each splits into 2 again, so it takes 2 rounds.
1 ≤ len(sizes) ≤ 100,0001 ≤ sizes[i] ≤ 1,000,000Dividing one size by every candidate up to its square root takes about 1,000 tries for a prime near 1,000,000. For 100,000 such sizes that is about 100 million tries. A table of the smallest prime factor of every number up to 1,000,000 costs about 2,100,000 steps once. Then every size is factored in at most 19 steps.