implementation of Project Euler #138