Test and brute force algorithm