Solves P3 using nested loops, need to refactor for higher values