Build the table once
transcript
A hundred thousand shard sizes arrive. Each is a number up to a million. Split each size into prime factors. How fast can you do it?
There are a hundred thousand sizes. Each needs up to a thousand divisions. That is a hundred million divisions in all. Far too slow in Python.
The sizes share their numbers. So fill one table first. Then every query is a read.
Walk the primes in order, from two upwards. Write each prime into its multiples. Skip the numbers already marked. The table is ready in one pass.
Read the smallest prime of the size. Divide it out of the size. Read the next smallest prime, and divide again. Repeat. At most nineteen steps.
Find the largest value in the input. Fill the table once, with a sieve. Then read it for every query. The work moves out of the queries. A hundred thousand queries become cheap. Full breakdown in the lecture.