How to work with 100+digit numbers

  • Thread starter Thread starter Edi
  • Start date Start date
  • Tags Tags
    Numbers Work
Click For Summary

Discussion Overview

The discussion revolves around techniques and methodologies for working with extremely large numbers, particularly those exceeding standard data types like Int or _int64. Participants explore various programming approaches, algorithms, and existing libraries that facilitate operations on numbers with hundreds or thousands of digits, with applications in areas such as cryptography.

Discussion Character

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

Main Points Raised

  • One participant suggests using arrays or strings to represent large numbers and proposes a custom function for digit-by-digit arithmetic, noting concerns about memory efficiency.
  • Another participant describes two basic approaches for handling large numbers: using machine instructions with carry and emulating carry in software, highlighting the trade-offs in speed and memory usage.
  • A participant mentions that many programming languages, such as Python, Groovy, and Java, have built-in support for arbitrary-precision arithmetic.
  • Code is provided by a participant demonstrating how to add large numbers using 64-bit limbs, with attention to carry handling.
  • One participant suggests using unsigned arithmetic and shifts to manage carry bits, proposing a "BigNumber" class with operator overloading for addition.
  • Another participant discusses advanced algorithms like Karatsuba's binary splitting and FFT methods for multiplying large numbers, noting their applicability for numbers with millions of digits.
  • Recommendations for existing libraries are made, including GMP for C++ and MIRACL for non-commercial use, with a suggestion to explore cryptography libraries for additional resources.

Areas of Agreement / Disagreement

Participants express a range of ideas and methods for handling large numbers, with no clear consensus on a single best approach. Multiple competing views and techniques are presented, indicating an ongoing exploration of the topic.

Contextual Notes

Some discussions involve specific algorithms and implementations that may depend on particular programming languages or environments. The effectiveness of various methods may vary based on the size of the numbers being manipulated and the computational resources available.

Who May Find This Useful

This discussion may be useful for programmers, computer scientists, and mathematicians interested in numerical methods, cryptography, and software development involving large number computations.

Edi
Messages
176
Reaction score
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 :-p ), 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
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.
 
// 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;
}
 
.Scott, I would use unsigned arithmetic and shifts to get the carry bit. That eliminates conditional statements.
 
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.
 
.Scott said:
Actually, I would have search for some existing implementation of this class.

:thumbs:
 
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:
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.
 

Similar threads

Replies
14
Views
3K
Replies
25
Views
4K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
Replies
3
Views
2K
Replies
11
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K