I'm using ulong for the factorial of 100, but it still overflows

前端 未结 2 2085
礼貌的吻别
礼貌的吻别 2021-01-29 06:34

So my task is: I have the number 100, and I have to print the sum of the digits of it\'s factorial.

So I wrote the code, found a nice way of summing the digits but my c

2条回答
  •  忘掉有多难
    2021-01-29 07:04

    All integer types have limits. unsigned long int increased the upper limit. But apparently not nearly far enough. As the others have said in comments, ulong is to short by 100+ orders of magnitude.

    For such huge numbers there is two options:

    1. use floating point numbers. Asuming you can live with their inherent inprecision and all the other Floating point stuff.
    2. Use BigInteger. That one will only run into limits like the max objects size or adresseable RAM. So you should be save up to 2 GiB or so.

    Personally I tend to squeeze operations into the BigInt rather then use Floating point numbers. But that is a personal mater.

提交回复
热议问题