Non-recursive algorithm with N numbers