inefficient solution for prob12

getting memory error