Resolution of problem 4 using brute force and java