2

How can I store an integer of value in 10^100 in C? I was trying to solve a question in competition the input data in range of 10^100 which surpasses the data size limit of even unsigned long int . How do I store and process it?

  • 6
    You'll need a bignum library, or at the least implement your own big integer arithmetic. Check out [GMP](https://gmplib.org/), for instance. – Blaze Dec 03 '19 at 13:21
  • 2
    Just because the input is an integer, doesn't *necessarily* mean it's a good idea to store it as an integer, or even at all (there are various competitive programming problems with an integer input where you can consume the input digit-by-digit and never store the whole thing at once). It depends, and if you explain more of the problem you can get help with judging that too. – harold Dec 04 '19 at 14:04
  • there are a lot of duplicates in the [tag:bigint] tag. [which datatype should i use to store a variable 10^200 in C language?](https://stackoverflow.com/q/23588717/995714), [Handling large numbers](https://stackoverflow.com/q/117429/995714), [What are various methods to store very large integer value in a variable with less compilation time when doing operation on that variable](https://stackoverflow.com/q/49085108/995714)... – phuclv Dec 04 '19 at 16:15

1 Answers1

0

Blaze's comment is spot-on. Here is a more theoretical exposition of the same idea.

Integers up to 10^100 can be written in base-10 notation with 100 or fewer digits (well, 101 digits for the number 10^100 itself). We can store the base-10 representation of these integers as strings of length up to 100 (101 if inclusive on the 10^100 side). Simple arithmetic operations you learned in school - add with carry, subtract with borrow, long multiplication and long division, etc. - are actually algorithms on the strings of digits, so these can be implemented on strings representing numbers up to 10^100 and beyond. The length of strings can go up to at least ~2^16 symbols, and probably more if you don't require the whole thing to be in one string variable at a time. If you need numbers whose base-10 representations can't be represented in this way - by storing the base-10 representations in collections of strings - good luck!

Patrick87
  • 27,682
  • 3
  • 38
  • 73
  • Strings in C can have any length as long as the memory is available. There is no limit at 2^16 characters. Or did you mean anything else? – the busybee Dec 04 '19 at 15:01
  • @thebusybee No, that's what I meant, and I think you're right about that not being a limit in general. I added "at least" since I wasn't sure and I actually didn't even notice the `c` tag until you mentioned it. – Patrick87 Dec 04 '19 at 15:04