# Why do Julia BigInt iterative functions overflow?

**URL:** <https://discourse.julialang.org/t/why-do-julia-bigint-iterative-functions-overflow/110674>\
**Category:** General Usage\
**Tags:** question\
**Created:** [February 23, 2024, 10:00pm UTC](https://discourse.julialang.org/t/why-do-julia-bigint-iterative-functions-overflow/110674 "2024-02-23T22:00:40Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 23, 2024, 10:13pm UTC](https://discourse.julialang.org/t/why-do-julia-bigint-iterative-functions-overflow/110674/5 "2024-02-23T22:13:59Z")

</div>

> [@mikmoore](#):
>
> Some languages utilize “tail call optimization” to eliminate the excess memory use of some simple recursive functions

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](https://discourse.julialang.org/t/tail-call-recursion/87847/19) — many people don’t realize how [narrowly applicable it is](https://discourse.julialang.org/t/tail-call-recursion/87847/17).)

For the particular case of the factorial function, of course, you should just call the [built-in `factorial` function](https://docs.julialang.org/en/v1/base/math/#Base.factorial), which supports `BigInt` and is highly [optimized by the GMP library](https://gmplib.org/manual/Number-Theoretic-Functions). `factorial(big(1000000))` takes about 0.1 seconds on my laptop. It [isn’t implemented using explicit recursion](https://github.com/alisw/GMP/blob/2bbd52703e5af82509773264bfbd20ff8464804f/mpz/oddfac_1.c#L266-L419), though the underlying [mathematical algorithm is formally recursive](https://oeis.org/A000142/a000142.pdf).

---

_[View the full topic](https://discourse.julialang.org/t/why-do-julia-bigint-iterative-functions-overflow/110674)._
