Solution to problem 51

This is very slow and obviously permutations are checked too often but it works...