Modify brute force function to be iterative rather than recursive