working solution for problem 8
faster processing for problem 8, first try at problem 9