Wow, that's a lot of code for something that's really simple.

I suppose you've already run both it and the C version, to verify that they produce the exact same output for a given input, right?

Cheers