Refactored Euler Problem 5