How to work with 100+digit numbers

In summary, Scott is trying to find a way to work with super-large numbers without wasting memory or using inefficient methods. He is open to using existing libraries or developing his own.
  • #1
Edi
177
1
I am studying programming for the first year so I don't have whole lot of experience. I am interested in how to work with super-large numbers, numbers that are much, much larger then the included Int or _int64. I mean some 100, 1000 .. 17 million (largest prime) digit numbers. How do they do that? I understand that my home pc has no chance at calculating some of the monster primes (unless I come up with a genius, simple way to find primes :tongue: ), but I might do some other manipulations with monster numbers. Cryptography and what not.
I figure one way might be to use an array (or a string) where each iteration is a single digit along with a custom made function to do maths digit by digit (kinda like on-paper multiplication)..
That might be one way, but then each element of the array would consume 4 bytes (2 if I define it as short int ) and I only need .. half a byte to represent a digit from 0 to 9.A lot of memory wasted. I might resolve that problem by working in a higher base (2 byte max value base) ..
Maybe, but how do professionals do it? Even ms calculator can do calculations with larger numbers...
 
Technology news on Phys.org
  • #2
There are two basic approaches. One uses whatever machine instructions available on the given platform to perform arithmetic operations with "carry". Another emulates "carry" in software. For example, you can represent your big number using a sequence of 64 bit "limbs", and each limb uses only 63 bits to store data. When you add two limbs, you then check that the 64-th bit in the result is set or not. If set, that's your "carry" flag. The second approach is generally slower and uses more memory, but it is portable.
 
  • #4
// Using voko's numbers (see post #2)
// Each input array specifies an integer with nWords*63 bits. The words are grouped in big endian form.
// Integer at pnB is added to the integer at pnA.
AddBigNums(int nWords, __int64 *pnA, const __int64 *pnB)
{
int n, nCarry;
__int64 nSum;

nCarry = 0;
for(n=0;n<nWords;n++) {
nSum = pnB[n] + pnA[n] +nCarry;
if(nSum<0) {
nSum &= 0x7FFFFFFFFFFFFFFF;
nCarry = 1;
} else {
nCarry = 0;
}
pnA[n] = nSum;
}
 
  • #5
.Scott, I would use unsigned arithmetic and shifts to get the carry bit. That eliminates conditional statements.
 
  • #6
voko said:
.Scott, I would use unsigned arithmetic and shifts to get the carry bit. That eliminates conditional statements.
I was trying to be explicit. If I was actually implementing something, I would have created a "BigNumber" class; I would have used all 64 bits; and I would have used operator overloading for "+".

Actually, I would have search for some existing implementation of this class.
 
  • #7
.Scott said:
Actually, I would have search for some existing implementation of this class.

:thumbs:
 
  • #8
Edi said:
I am interested in how to work with super-large numbers.
It's a complicated combination of algorithms like Karatsuba's binary splitting for doing large multiplies, and performing the math modulo some prime number (less than 32 bits or 64 bits depending on the word size), and FFT like methods. Example description from apfloat (a downloadable source code library for large number math):

http://www.apfloat.org/ntt.html

Another link: (look for the FFT stuff):

http://numbers.computation.free.fr/Constants/constants.html

These methods are overkill for 100 to 1000 digits, but for large numbers (1 million + digits) they are fast.
 
Last edited:
  • #9
Well if your want to look at a good existing library, GMP is probably the best. And it's written in C++.

just do a search for it in google.
 
  • #10
If it is for non-commercial use, then MIRACL is a good library to use (it's free for non-commercial use).

I've used it in the past and it had good performance.

Other suggestions include looking at cryptography libraries which often contain big number libraries out of necessity.
 

1. How do I add or subtract 100+ digit numbers?

Adding or subtracting 100+ digit numbers can be done by using a calculator or a computer program that is capable of handling large numbers. If you are doing it manually, it is important to keep track of the place values and carry over any extra digits to the next column.

2. Can I multiply or divide 100+ digit numbers by hand?

Multiplying or dividing 100+ digit numbers by hand can be extremely time-consuming and prone to errors. It is recommended to use a calculator or a computer program for these operations. However, if you need to do it by hand, breaking the numbers down into smaller parts and using long multiplication or division can make the process more manageable.

3. What is the best way to store and manipulate 100+ digit numbers?

Storing and manipulating 100+ digit numbers can be challenging due to their size. One way to store them is by using a data structure called a "big integer" which can handle large numbers efficiently. To manipulate these numbers, you can use specialized libraries or functions designed for working with big integers.

4. Are there any shortcuts or tricks for working with 100+ digit numbers?

Unfortunately, there are no shortcuts or tricks for working with large numbers. The best approach is to use a calculator or a computer program that is designed to handle them. However, if you are working with smaller parts of the numbers, some mental math techniques like rounding or estimation can help speed up the process.

5. What are some practical applications of working with 100+ digit numbers?

Working with 100+ digit numbers is often necessary in fields such as cryptography, finance, and scientific research. For example, encryption algorithms use large numbers for secure communication, financial calculations involving large sums of money require handling large numbers, and scientific calculations may involve very precise and large numbers.

Similar threads

  • Programming and Computer Science
Replies
1
Views
936
Replies
1
Views
578
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
9
Views
1K
Replies
25
Views
3K
  • Programming and Computer Science
Replies
11
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
4
Views
729
  • Programming and Computer Science
Replies
5
Views
983
Back
Top