How Can I Implement Big Int Functionality in C++ for Project Euler Problems?

  • C/C++
  • Thread starter trollcast
  • Start date
  • Tags
    C++ Writing
In summary, the code in trollcast shows how to sum 100 numbers that are each 50 digits long to find the first 10 digits of the sum. The code uses a Bigint class that is part of the Boost library.
  • #1
trollcast
Gold Member
282
13
Hi,

I'm working my way through the project euler problems in c++ and I've got to one where you have to sum 100 numbers that are each 50 digits long to find the first 10 digits of the sum.

Obviously I could solve it by summing each of the figits in turn then work with them to deal with carry overs.

However I was wondering if there was a way I could write some code to implement something like Java's Big Int datatype?

I'm a bit of a noob with c++ and my google fu isn't working tonight so any links or help would be great.
 
Technology news on Phys.org
  • #2
Certainly it's possible to write your own Big Integer class in C++. Here is library that someone else has already written, if you don't want to reinvent the wheel:

https://mattmccutchen.net/bigint/
 
  • #3
There's an interesting solution to this problem. Given that you only require the first 10 digits of the sum, you can get away with only summing the first 11 digits of each number. Digits beyond the 11th will not affect the first 10 digits of the sum. With this in mind, you can solve the problem without using a Big Int implementation. I'll mention that I didn't realize this when I was attempting the problem, but read it in the forum after I had solved it.

Having said that, writing a Big Int library would be an interesting exercise. You need to perform addition, subtraction and other operations manually like you mentioned, one digit a time and carrying if necessary. Converting to a base higher than 10 is more efficient because you can represent numbers with less digits, and I think there are other tricks you can use too, perhaps someone can elaborate on exploiting binary operations and using bases that are powers of 2.

Here's some code that implements addition in base 10, untested but I think it works ok.

Code:
int add(num1, num2)
{
    int result = 0;
    int magnitude = 1;
    while (num1 > 0 && num2 > 0)
    {
        int digit1 = num1 % 10; // get the least significant digits of each number
        int digit2 = num2 % 10;
        result += (digit1 + digit2) * magnitude; // add the sum of these digits multiplied by their magnitude according to position in num1/num2 to our running total
        magnitude *= 10; // increment magnitude by power of 10
        num1 /= 10; // integer division drops the last digits
        num2 /= 10;
    }
    return result;
}
 
  • #4
Thanks guys,

jbunniii: I've already solved the problem using Python but I was more interested in how you could create a class to handle very large numbers.

Adyssa: Thanks for the example, I was thinking on something similar using C like strings to store the digits and then working with each element of the char array individually. There maybe some value to be gained out of maybe converting the decimal to maybe hexadecimal or even base 32 since these represent 4 and 5 binary bits respectively so it would be quite easy to implement binary additon and stuff for them.
 
  • #5
I wouldn't use C strings - I would use the std::deque template (http://www.cplusplus.com/reference/deque/deque/), or maybe std::vector, depending on your exact implementation. Conversion to hexadecimal wouldn't make a lot of sense unless you were really worried about space, and trying to fit exactly 2 digits into each byte. In general, I would go for the simple implementation rather than an unnecessarily optimised one.
 
  • #6
Trollcast: C++ has two main "standard" libraries for general purpose programming, these being the standard library itself and the Boost library. Boost has a Bigint in Numerics. You might want to check out the source code.

Python really spoils you, it does so much out of the box and the standard library is amazing too.

Of course on this forum people probably use the domain-specific math and sci libs.
 
  • #8
Adyssa said:
There's an interesting solution to this problem. Given that you only require the first 10 digits of the sum, you can get away with only summing the first 11 digits of each number. Digits beyond the 11th will not affect the first 10 digits of the sum.

I just noticed that. Is there a 'proof' of that? It is hard to believe. What about the case of 1.9999999 etc?
 
  • #9
You may also have a look at InfInt's source code.
 
  • #10
Adyssa said:
There's an interesting solution to this problem. Given that you only require the first 10 digits of the sum, you can get away with only summing the first 11 digits of each number. Digits beyond the 11th will not affect the first 10 digits of the sum.
Why would the 11th digit have any effect on the first 10 of the sum?
 

1. How do I declare a big integer variable in C++?

In C++, big integers are typically represented using the long long data type, which can hold integers up to 9,223,372,036,854,775,807. To declare a big integer variable, you can use the following syntax: long long myInt;

2. How do I assign a value to a big integer variable in C++?

You can assign a value to a big integer variable using the assignment operator (=). For example: myInt = 123456789; or myInt = myOtherInt; where myOtherInt is another big integer variable.

3. Can I perform arithmetic operations on big integers in C++?

Yes, you can perform arithmetic operations (addition, subtraction, multiplication, division) on big integers in C++. However, it's important to make sure that the result of the operation does not exceed the maximum value that can be stored in a long long variable.

4. How do I output a big integer variable in C++?

To output a big integer variable in C++, you can use the cout statement. For example: cout << "My big integer is: " << myInt << endl; where myInt is the name of your big integer variable.

5. What happens if my big integer value exceeds the maximum value of a long long variable?

If your big integer value exceeds the maximum value of a long long variable, it will result in an overflow error. This means that the value will wrap around to the minimum value of a long long variable and continue counting from there.

Similar threads

  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
3
Views
1K
Replies
4
Views
1K
  • Programming and Computer Science
Replies
3
Views
888
  • Programming and Computer Science
Replies
31
Views
6K
  • Programming and Computer Science
Replies
10
Views
3K
Back
Top