Unelegant brute force method of solving problem 5