+ 4

Storing and printing an integer value with 20 or more digits

how to do this?

10th Feb 2018, 10:22 PM
Mich
Mich - avatar
5 Answers
+ 12
There is a concept of big integer that can handle any number of digits. There are a few library floating around in C++ or you could write your own. Personally, I'd write my own as it would be nice challenge.
10th Feb 2018, 10:36 PM
John Wells
John Wells - avatar
+ 3
@Jayden LeCorps There are two methods: a) Strings that hold digits. This is the simple, but inefficient way. b) pointers, bitwise operators and dynamic memory management using malloc. This is the efficient, but cancerous way. It's a shame that the C++ language/standard library doesn't feature arbitrary length data types because, aside from that, it's really complete.
10th Feb 2018, 10:55 PM
Vlad Serbu
Vlad Serbu - avatar
+ 3
@Jay Matthews That would be a way to do it. And probably the best way if you don't want to get your hands dirty with bitwise. But how do you calculate the carry-over of say 999,999,999*999,999,999? The only way I can think to do it is to make multiplication a for loop for repeated addition, and that's just not very efficient.
10th Feb 2018, 11:31 PM
Vlad Serbu
Vlad Serbu - avatar
+ 2
thank you all for your help
11th Feb 2018, 3:32 PM
Mich
Mich - avatar