Why do Julia BigInt iterative functions overflow?

Even if Julia had TCO, it wouldn’t work for the FACTORIAL(n) function here because the recursive call is not in tail position. (This seems to be a widespread misapprehension about TCO — many people don’t realize how narrowly applicable it is.)

For the particular case of the factorial function, of course, you should just call the built-in factorial function, which supports BigInt and is highly optimized by the GMP library. factorial(big(1000000)) takes about 0.1 seconds on my laptop. It isn’t implemented using explicit recursion, though the underlying mathematical algorithm is formally recursive.

10 Likes