I would consider saving in an array all numbers (prime or otherwise) encountered so far. Whenever your current chain matches one of those you can skip to the next chain knowing it will eventually end in 3.
I suspect that most will abort a lot sooner.
I believe the astutue programmers call this memoization.
|