working solution of prob 5, will try to make it faster