3 ms·
Wow, that is clever. Not doing more than you need to - for the same effect - kind of thing. Thanks for sharing. I've been playing around with various simple al
by vram22 8y ago
Wow, that is clever. Not doing more than you need to - for the same effect - kind of thing. Thanks for sharing.
I've been playing around with various simple algorithms for different tasks, over a period, and been noticing that there is a lot of scope for improving efficiency, even if by small amounts, sometimes by a bit more, by making various tweaks to the logic, often including early termination techniques.
A well-known example of early termination is: to find if a number n is prime, instead of checking whether it is divisible by all the integers from 2 to n - 1, you can just check for those up to the square root of n. (There are more optimizations on those lines possible, of course.)
I've found that there can be many quick wins in this area, often in various algorithms that process arrays. For example, problems involving arrays on Rosetta Code.