DEV Community

Liam Foster
Liam Foster

Posted on

Finding all factors of n in O(sqrt(n)) with a tiny JS function

Quick one for beginners: you never need to test past the square root. Each divisor found below sqrt(n) gives you its pair for free, which is why a simple loop up to Math.sqrt(n) is enough. I built a small reference page around this idea here: factor calculator. Handy when you want to sanity-check homework without doing the loop by hand every time.

Top comments (0)