It looks like you're new here. If you want to get involved, click one of these buttons!

Here’s a factorial function. This happens to take advantage of Lua’s tail-call elimination:

```
function factorial(n, a)
if n == 0 then
return a
end
return factorial(n - 1, a * n)
end
print(factorial(21, 1))
```

...except with 21 and above, I don’t see the proper result. Are the numbers too large? Any way to see large numbers in Lua, or does it require a separate library?

Thanks.

Tagged:

## Comments

Codea uses 64 bit math and the largest number is 9,223,372,036,854,775,807.

Try this code and you’ll see it go from positive to largest number to negative which is correct.

On my TI-89 Titanium calculator, it will do 299! and show a 613 digit number. It will do 449! and give a result of 3.85193051803 e997 . After that it just give you what you entered. The largest integer number on the calculator is 614 digits.

If I use the WolframAlpha app on my iPad, I don’t know the largest factorial it will do.

I believe there are Lua libraries available to support large numbers/arbitrary precision, though I’ve never used one (yet).

I just remembered that I wrote a factorial program. Enter the factorial and tap the screen. Doing factorial 12345 takes 1.7 seconds and prints 45,151 digits. I’m not sure how large a factorial it will do, but 123456 takes 228 seconds on my iPad Air 3 and prints 574,965 digits. So just going up 1 digit increased the size and time by a lot.

Here’s an updated version of my factorial program. It’s best to run it in portrait mode to show more of the print area.

While we’re looking at large numbers, here’s a program that will multiply 2 strings of numbers. This example multiples 2 strings of 1600 digits. There’s no checking, so don’t put anything other than a number in the string. Run in portrait mode for the larger print area.

What’s nice around this forum is your creativity. Fun to see you take a posted question and watch you play with it. Codea is built for this type of quick turn-around. Thanks!

@brianolive Thats what’s fun with Codea. I try to answer the questions asked, but I also like to throw some examples that I hope will give others something to explore. That’s one way to learn.

@dave1707 do not forget that Codea will not handle the print function with too large strings (web pages from 'data' in http.request, this here, etc).

Edit: Also added this little info on the Lua article.