Why is division more expensive than multiplication?

CPU’s ALU (Arithmetic-Logic Unit) executes algorithms, though they are implemented in hardware. Classic multiplications algorithms includes Wallace tree and Dadda tree. More information is available here. More sophisticated techniques are available in newer processors. Generally, processors strive to parallelize bit-pairs operations in order the minimize the clock cycles required. Multiplication algorithms can be parallelized quite … Read more

Is a list (potentially) divisible by another?

Build bipartite graph structure – connect a[i] with all its divisors from b[]. Then find maximum matching and check whether it is perfect matching (number of edges in matching is equal to the number of pairs (if graph is directed) or to doubled number). Arbitrary chosen Kuhn algorithm implementation here. Upd: @Eric Duminil made great … Read more

Why doesn’t ‘d /= d’ throw a division by zero exception when d == 0?

C++ does not have a “Division by Zero” Exception to catch. The behavior you’re observing is the result of Compiler optimizations: The compiler assumes Undefined Behavior doesn’t happen Division by Zero in C++ is undefined behavior Therefore, code which can cause a Division by Zero is presumed to not do so. And, code which must … Read more