1

So I am working on a competitive programming problem where you take two numbers base two and base three, and then do some operations on them. I got the code implementing correctly, however it doesn't work with large inputs. For example, when I try to input 10010111011100101101100011011 (base two) and 211010102022001220 (base three). This is because I am inputting them as a regular integer, then converting them to their actual base 10 value.

This is my conversion function (just for bases 2 and 3)

int conv(int base, ll n){
int result = 0;
if(base == 2){
    int a = 0;
    while(n > 0){
        if(n % 2 == 1){
            result += pow(2, a);
        }
        a++;
        n /= 10;
    }
    return result;
}
else if(base == 3){
    int a = 0;
    while(n > 0){
        result += (n%10)%3 * pow(3, a);
        a++;
        n /= 10;
    }
    return result;
}
return result;

When I run this function on really small numbers like conv(2, 1010), it works; I get 10 (converting 1010 base 2 into base 10)

However, if I want to take in 10010111011100101101100011011(base 2), my code doesn't seem to work. Is there any other way I can do this?

  • 2
    `10010111011100101101100011011` (base 10) is too long to fit in standard C++ integer types. Read them as strings. – MikeCAT Oct 24 '20 at 02:29
  • How do you create the function if it were based on strings? – DragonFish12345 Oct 24 '20 at 02:34
  • 1
    The same way you would create any other function: write the code for it, test it, see if it works, if not figure out why not, fix it, try again, and so on. Unfortunately, stackoverflow.com is not a C++ tutorial site. For more information how to do many things in C++, [a good C++ textbook](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) will be your best resource. – Sam Varshavchik Oct 24 '20 at 02:38

2 Answers2

2

Depending on what particular operations you want to perform on your base-2 and base-3 numbers, it may be feasible to just operate on string values. The advantage is that you are virtually unbounded.

Read your input numbers as strings. Write a function to verify that a given string is a valid number in a given base. Then write those operations to work on strings. It will be easy for addition and subtraction, but will require a few more steps for multiplication and division. If you need to handle trigonometry and transcendental functions, then it will be a difficult but doable math problem. Basically you want to implement exactly the steps you undergo when you perform the operations by hand, complete with carrying and borrowing.

Howlium
  • 1,218
  • 1
  • 8
  • 19
0

There are different tricks of dealing with large int in cpp. In general you can use boost cpp_int.

cpp_int does not have a [theoretical] maximum value. Since C++ can directly use pointers, so the maximum size of cpp_int in C++ is proportional to the maximum memory range that a pointer can address—which in 64-bit architectures is usually-but-not-always 2^64-1; or in other words, the maximum value of cpp_int is 2^64-1, give-or-take an order of magnitude depending on how they implement the sign.

Black Magic
  • 79
  • 1
  • 4
  • 1
    Can you explain what this has to do with the question that was asked, here, which is not even about Python? – Sam Varshavchik Oct 24 '20 at 02:38
  • 1
    @SamVarshavchik obviously there was temporary confusion. The answer has been completely replaced. I'm not convinced it's a good answer yet, but at least it's relevant to the problem. – Mark Ransom Oct 24 '20 at 03:18