I agree with Portia and NotherBen that it's likely related to anxiety. It may also have something to do with the buzzing mind of a gifted child.

Maybe introducing him to meditation and mindfulness for children would help? If you're interested, look up "Spaghetti Toes" to see if it's age appropriate. We did it last year for a while with DS6, and I forget the age range.

At the very least, if he could stretch out the counting instead of looping (for example, count to 1000 instead of to 10 over and over again), it might help him calm down instead of getting worked up.