1
I saw a report about the discovery of a prime number that contains 22 million digits. How could a variable contain a number, for example, with 100 million digits.
1
I saw a report about the discovery of a prime number that contains 22 million digits. How could a variable contain a number, for example, with 100 million digits.
4
To put a very large number in memory, simple: save the digits as a large string
, or what is closer to the machine, the number in binary format as an array of byte
.
What you’re probably asking is how to put that number into memory in the way it behaves as a "common" number. For these cases there are data structures (or classes) that store the number in memory in any representation (probably byte[]
), with public methods that allow to manipulate this mass of data as if it were a "pure number".
Take a look: https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html (Java) and https://msdn.microsoft.com/pt-br/library/system.numerics.biginteger(v=vs.110). aspx (C#).
Other languages will have similar classes or structures. Actually, if you look for BigInteger
(or BigDecimal
), you will see that there are several different implementations, from third parties, of this type of class, in the most varied languages. That is, it is possible to create this kind of "big number" in the hand, without depending on the language that is working. In the end it’s just a data structure (or class), which happens to do the same thing as the native numbers.
3
Although it is technically possible to put the number into a variable, this is hardly done. Usually allocate it to memory and the variable just points to it. At least this would be necessary if you really need to store all the digits. It is not always (see Bacco comment). It has ways of representing the number without saving all its digits.
There are several ways to store all the digits as well. An obvious one would be to do the same thing as to save a text of 100 million characters (a String
. Only in this case it would only be numerical digits.
All techniques will normally have to use some array bytes. The encoding of this array may vary according to each need. Even if the number has some specific encoding.
It is often necessary to think beyond memory. If the number is too large it may not fit in memory. Even with the possibility of virtual memory, it may be useful to look for a solution designed to work with this number on disk and have more control of how this is done.
Some languages have their own type that already does this. The type is usually called BigInteger
or something like that.
Thank you all for the clarifications!! I will take a look at the links and research the tips given.
Browser other questions tagged variables memory whole
You are not signed in. Login or sign up in order to post.
These numbers don’t usually have "a lot of digits". They would have if they were written "in full", but their representation is short like this: 2 216091-1. Hypothetically, if you really had to work with multiple numbers of that digit by digit, you wouldn’t need variables, but a stream, which can be on disk, so you could "run" through it to do operations, and you wouldn’t have to have everything in memory. If you want the example number above in full, here is the link: http://bigprimes.net/pages/archive/mersenne/M31.txt - I took this as a short example, only 65050 digits.
– Bacco
I believe that that question is related to yours and can bring some explanations as well.
– tayllan