Slightly optimized brute force solution

I resisted using pen and paper.