Nice one. Running the code in a profiler might have revealed the true slow spot.
Project Euler is cool! My first programs on the Apple II were all math diversions like these. The problems also remind me of the math contests put out by the CEMC that I used to write in high school.