0

I have a problem where I need to work with variables

1 <= N <= 10^80

One of the test-cases works with a value:

3141592653589793238462643383279502884197169399375

and using unsigned long long int, the program sees this value maximum as:

18446744073709551615

Apparently, I need to store a value greater than that.

How do I solve this problem?

#include <iostream>

using namespace std;

int main()
{

unsigned long long int N;
cin >> N;
unsigned long long int Z;
int result = 0;

unsigned long long int num = N;

while (N > 0) {
    Z += N % 10;
    N /= 10;
}

while (Z % 9 != 0) {
    Z += num;
    result++;
}

cout << Z;

return 0;
}
N.T.
  • 99
  • 6
  • use `std::strings` – The Philomath Dec 01 '19 at 11:29
  • @ThePhilomath how exactly should I implement that? – N.T. Dec 01 '19 at 11:30
  • Just storing it? Or do you want to do arithmetic on it? – President James K. Polk Dec 01 '19 at 11:31
  • @JamesReinstateMonicaPolk I added my code above. – N.T. Dec 01 '19 at 11:32
  • 2
    And ...? So, what, N is the huge number you want to manipulate? If so you should probably look first at the GMP library, if you can live with its licensing. – President James K. Polk Dec 01 '19 at 11:35
  • @ThePhilomath it's not the most efficient way – phuclv Dec 01 '19 at 11:42
  • @N.T. you need to show what you've tried, otherwise this isn't suitable on SO. There are already a lot of duplicate questions in the [tag:bigint] tag – phuclv Dec 01 '19 at 11:43
  • 1
    @James: Or MPIR. I've always preferred that just due to the fact GMP (at least when I dealt with them) were stunningly unhelpful whereas the MPIR folk seemed to go out of their way to assist. YMMV. – paxdiablo Dec 01 '19 at 11:44
  • 1
    duplicates: [How to implement big int in C++](https://stackoverflow.com/q/269268/995714), [Can I Make my Own Custom Data Type Larger than the Ones in C++?](https://stackoverflow.com/q/12102621/995714), [Big integer in C or C++](https://stackoverflow.com/q/4827538/995714), [C++ Add and Subtracting 100 digits numbers](https://stackoverflow.com/q/21146276/995714)... – phuclv Dec 01 '19 at 11:46
  • @phuclv: Interesting that it's almost the exact same question, with the same long string of the digits of pi for the example constant. – President James K. Polk Dec 01 '19 at 11:57
  • In your example code, Z is used before set, and result is set but never used. – John Zwinck Dec 01 '19 at 12:04

1 Answers1

0

In my opinion you should use big integer. There are few ways. First, using C - Library, which has C++ interface. The GNU Multiple Precision Arithmetic Library: http://gmplib.org/ The second way - you should implement your own BigInteger Class.

    template<class Type>
    class BigInt
    {
        typedef typename Type BT;
     protected: 
        std::vector<Type> value_;
    };

In other words you just split the big number and record each part into the vector.

JuiceFV
  • 171
  • 11
  • Is there a simpler way to approach this problem? – N.T. Dec 01 '19 at 13:41
  • @N.T. I think no. CPU interpretate your input in byte-code for example 16 - it's 10000 or 0xf. For the "int" we have only 4 bytes, thus we should to split our big-number and record it into the array. All Interpates of the BigInteger looks like that. – JuiceFV Dec 01 '19 at 18:19