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

  • Context: C/C++ 
  • Thread starter Thread starter trollcast
  • Start date Start date
  • Tags Tags
    C++ Writing
Click For Summary

Discussion Overview

The discussion revolves around implementing Big Integer functionality in C++ for solving Project Euler problems, particularly focusing on summing large numbers and extracting significant digits from the result. The conversation includes various approaches, coding examples, and considerations for handling large integers.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant suggests summing the digits of large numbers directly, considering carry overs, while seeking a way to implement a Big Int class similar to Java's.
  • Another participant provides a link to an existing Big Integer library, indicating that it is possible to create one in C++.
  • A different viewpoint is presented, noting that only the first 11 digits of each number need to be summed to determine the first 10 digits of the total, which could simplify the problem without requiring a Big Int implementation.
  • One participant shares a code snippet for adding two numbers digit by digit, emphasizing the need for manual handling of carries.
  • Another participant expresses interest in using C-style strings to store digits, suggesting that converting to bases like hexadecimal or base 32 could facilitate binary addition.
  • A suggestion is made to use C++ containers like std::deque or std::vector instead of C strings for storing digits, advocating for a simpler implementation over an overly optimized one.
  • One participant mentions the availability of a Big Int implementation in the Boost library, noting the differences in ease of use between Python and C++.
  • Another participant shares a link to a resource for implementing Big Int functionality, claiming it is user-friendly.
  • Several participants reiterate the point about summing only the first 11 digits, with one questioning the validity of this approach and seeking clarification on its implications.

Areas of Agreement / Disagreement

Participants express varying opinions on the necessity of a Big Int implementation, with some advocating for it while others suggest alternative methods. The discussion about summing only the first 11 digits remains contested, with some participants questioning its validity and seeking further proof.

Contextual Notes

There are unresolved assumptions regarding the mathematical implications of summing only the first 11 digits and how they affect the first 10 digits of the total. The discussion also reflects differing preferences for data structures and implementation strategies.

trollcast
Gold Member
Messages
282
Reaction score
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
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/
 
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;
}
 
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.
 
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.
 
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.
 
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?
 
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?
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 29 ·
Replies
29
Views
8K
  • · Replies 29 ·
Replies
29
Views
10K
  • · Replies 8 ·
Replies
8
Views
6K
Replies
4
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K