C++: Brute force algorithm written