Large Numbers Requiring More than 64 bit Representation

笑着哭i 提交于 2020-04-29 10:40:19

问题


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?


回答1:


As you mentioned in your question, you should use BigInteger.

They can be as large as you need - until you run out of memory.




回答2:


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




回答3:


you are looking either for the class BigDecimal or if you just need integers, than BigInteger. Its arbitrary precision, so the size changes based on how big the numbers are that you input




回答4:


To find a square root of a BigInteger you have to do a google search with "-StackOverflow" answers omitted. https://www.google.com/search?q=java+extract+root+bignum&ie=utf-8&oe=utf-8#q=java+root+BigInteger+-stackoverflow.com+-stackexchange.com yields the first link to http://faruk.akgul.org/blog/javas-missing-algorithm-biginteger-sqrt/ Here is one algorithm. Java is supposed to be "write once, use everywhere". Well, some SO users believe that you must reinvent the wheel. Ask them for a module they answer "smells like a homework, do it yourself". And yes, BigInteger is half baked. It can't do logarithms or roots out of the box. It is extremely slow. The algorithm is given below.

BigInteger sqrt(BigInteger n) {
  BigInteger a = BigInteger.ONE;
  BigInteger b = new BigInteger(n.shiftRight(5).add(new BigInteger("8")).toString());
  while(b.compareTo(a) >= 0) {
    BigInteger mid = new BigInteger(a.add(b).shiftRight(1).toString());
    if(mid.multiply(mid).compareTo(n) > 0) b = mid.subtract(BigInteger.ONE);
    else a = mid.add(BigInteger.ONE);
  }
  return a.subtract(BigInteger.ONE);
}



回答5:


import java.util.Scanner; import java.math.BigDecimal;

public class Ha40 {

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int i = scan.nextInt();
    long d=scan.nextLong();
    String st=" ";
    st=scan.next();
    st+= scan.nextLine();
    // Write your code here.

    System.out.println("String: " + st);
    System.out.println("Double: " + d);
    System.out.println("Int: " + i);
}

}

input are:2147483647 235345345345.234534 fsdfsdf sdf but ans is not getting upto the mark



来源:https://stackoverflow.com/questions/10051324/large-numbers-requiring-more-than-64-bit-representation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!