I am using java and have to deal with numbers larger than long (which is 64 bits). What should I use? What is the size of BigInteger in java?
A 64-bit signed integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive). A 64-bit unsigned integer.
A 32-bit number can store 2^32 values, or 4,294,967,296. Meanwhile, a 64-bit number has 2^64 possible values, or a staggering 18,446,744,073,709,551,616. That's over 18.4 quintillion, which is so large that it's difficult to comprehend.
In computer architecture, 128-bit integers, memory addresses, or other data units are those that are 128 bits (16 octets) wide. Also, 128-bit central processing unit (CPU) and arithmetic logic unit (ALU) architectures are those that are based on registers, address buses, or data buses of that size.
There are 65,536 different unsigned 16-bit numbers. The smallest unsigned 16-bit number is 0 and the largest is 65535.
As you mentioned in your question, you should use BigInteger.
They can be as large as you need - until you run out of memory.
What is the size of BigInteger in java?
That's a little bit tricky. The problem is that there is no clear specification of the limit in the javadocs.
The class uses an int[]
to represent the magnitude. This means it could potentially represent numbers up to ((2^32)^(2^31 - 1)
.
The API has a method that returns the number as a 2's complement byte array. The limit for this is ((2^8)^(2^31 - 1)
.
The API has another method that returns the size of the number in bits ... as an int
. This implies a limit of 2^(2^31 - 1)
or maybe 2^(2^32)
.
In practice, these numbers are all so large that you will probably run into heap space limits (or CPU performance limits) first.
the problem is I have to find out the square root of a the number.
You should be able to find an algorithm for calculating square roots in your undergraduate maths text books (or Wikipedia). Coding it should be a simple task.
(I'd point you at example code, except that this smells like "homework", and I don't entirely trust the code that I found.)
Don't forget that most integers have an irrational square-root ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With