spiritway,
This is not a general purpose solution. It is addressing a specific mathematical problem that by its definition can't exceed 1000. Normally when I think something is bounded that I have no control over I stipulate that it should not <fill in the blank> and that I would want to know about it if it does.
Well, I meant no harm or insult in my comment. I've just found that when I write something for a very limited purpose, it often happens that I eventually come to regret making it so limited. Don't let me spoil the fun, though - if your example won't go beyond 1000 elements, then that's a useful bit of information. Good luck.