Refactored project euler problem 4.