improve loop runtime
Solutions for problems 036-040