Something is required to store numbers more than int64 can contain.

There is an idea to create an array of int, where the first element is one, the second is thousands, the third is millions, and so on. But this solution is very cumbersome and takes up a lot of space.

What other options or how to improve the proposed?

  • one
    See BigInteger. Represents an arbitrarily large signed integer. - WebMorda
  • type decimal - Alias

2 answers 2

The best option is to use BigInteger . It can store any, arbitrarily large number.

If you need something samopisopny for some reason, study how it works and invent a bicycle :)

Here is a description:

https://habr.com/post/207754/

Examining the constructors of the BigInteger structure can be concluded: if a number is placed in the int range, then it is stored in the _sign variable; if the number does not fit into the int range, then its sign is stored in the _sign variable (-1 for negative and 1 for positive), and the _bits array contains the coefficients ai of the long number decomposition with base 2 ^ 32.

    Your idea is correct, with the only difference that it is better to store the number not in the decimal number system, but in the system with a base of 10 9 or 2 32 - this will reduce memory consumption by 9 times.

    But if the goal is not training in algorithms of long arithmetic, but solving real problems, it is better to use the ready-made BigInteger class.

    • Already wanted to work with BigInteger, but I have no namespace System.Numerics? Maybe you know what to do? - Andrew
    • 2
      @Andrew add link to build System.Numerics.dll - Zergatul
    • @Zergatul Thank you. I'll try. - Andrew