Refactored to allow any number as the top limit

The algorithm is really slow though and increases time
exponentially.