How to sum large numbers?

后端 未结 3 2035
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-08 07:32

I am trying to calculate 1 + 1 * 2 + 1 * 2 * 3 + 1 * 2 * 3 * 4 + ... + 1 * 2 * ... * n where n is the user input. It works for values of n

3条回答
  •  失恋的感觉
    2020-12-08 07:59

    You could use a double, especially if your platform uses IEEE754.

    Such a double gives you 53 bits of precision, which means integers are exact up to the 53rd power of 2. That's good enough for this case.

    If your platform doesn't use IEEE754 then consult the documentation on the floating point scheme adopted. It might be adequate.

提交回复
热议问题