biginteger « biginteger « Java Data Type Q&A

1. Why is BIGInteger in JAVA not responding for higher powers?

When I try to find the value of BigInteger data type for 2 to power 23,000, I am not able to see the value. Upto 2 to power 22000, I could ...

2. Why can Haskell handle very large numbers easily?

Hugs> 94535^445

3. Java: How to use BigInteger?

I have this piece of code, which is not working:

BigInteger sum = BigInteger.valueOf(0);
for(int i = 2; i < 5000; i++) {
    if (isPrim(i)) {

4. Java workaround for BigInteger

I was working on a scenario wherein i had to implement BODMAS in Java and the operands could have as many as 1000 digits. So i chose to implement it in ...

5. What complexity are operations on Java 7's BigInteger?

What complexity are the methods multiply, divide and pow in BigInteger currently? There is no mention of the computational complexity in the documentation (nor anywhere else).

6. What data-structure should I use to create my own "BigInteger" class?

As an optional assignment, I'm thinking about writing my own implementation of the BigInteger class, where I will provide my own methods for addition, subtraction, multiplication, etc. This will be ...

7. Switch to BigInteger if necessary

I am reading a text file which contains numbers in the range [1, 10^100]. I am then performing a sequence of arithmetic operations on each number. I would like to use ...

8. BigInteger or not BigInteger?

In Java, most of the primitive types are signed (one bit is used to represent the +/-), and therefore when I am exceed the limits of this type, I can get ...

9. biginteger calculation problem

I am using the following code but the parameters are not passed to the methods.

BigInteger p = BigInteger.valueOf(0);
BigInteger u1 = obj.bigi_calc(g1, l);
In this g1,l are long values. The method is
private BigInteger bigi_calc(long ...

10. java: how for loop work in the case of BigInteger

I want to take Input from the user as Big-Integer and manipulate it into a For loop

BigInteger i;

for(BigInteger i=100000;i<=1;i--){


But it won't work can any body help me. ...

11. Get the integral part of a BigFraction, as a BigInteger

What is an easy way to get the integral part of a BigFraction as a BigInteger? Basically I want the same result that the intValue and longValue methods ...

12. Optimise arithmetic operations on very large numbers

I want to compute the function H(n) where

H(i)=H(i-1)×A+Ci mod B;

C is an array of n elements But it is taking too much time...any better way of doing this..Please help
public BigInteger H(int no)

13. Creating a BigInteger Method

Is there any way to create a BigInteger Method? I want to have a method of return type BigInteger.

14. Using BigInteger Multiply operator

I was wondering if there was a way to multiply BigInteger variables together, because the * operator cannot be applied to BigInteger. So I was wondering if it was possible to ...

15. Java, comparing BigInteger values

BigInteger bigInteger = ...;

if(bigInteger.longValue() > 0) {  //original code
    //bigger than 0

//should I change to this?
if(bigInteger.compareTo(BigInteger.valueOf(0)) == 1) {
    //bigger than 0
I need to ...

16. java using BigInteger and While

I have a problem

import java.math.BigInteger;
import java.util.*;
import java.lang.*;

public class medici {
   public static void main(String[] arg)  {

  BigInteger zac = new ...

17. Exponentiation of a Java BigInteger with a value lower than 1

Possible Duplicate:
How to do a fractional power on BigDecimal in Java?
I have a BigInteger A that I need to exponentiate with 1/b (b is ...

18. how can I use biginteger in java

i have this code and i want to change it to BigInteger

import java.util.*;
public class Euclid {
    long TIME;
    long start = System.currentTimeMillis();

19. BigInteger Homework Help Needed

I have been told I have to write a BigInteger class, I know there is one, but I have to write my own. I am to take either ints or a ...

20. How does BigInteger store its data?

I've been searching around for quite a while, and I've found almost nothing on how BigInteger actually holds its numbers. Are they an array of chars? Something else? And how is ...

21. OutOfMemoryError on BigInteger

I'm writing a polish notation calculator for BigIntegers (just *, ^ and !) and I'm getting an OutOfMemoryError on the line where I'm subtracting BigInteger.ONE to get the factorial to work, ...

22. Implementing BigInteger's multiply...from scratch (and making sure it's O(n^2))

As homework, I'm implementing Karatsuba's algorithm and benchmarking it against a primary-school-style O(n^2) multiplication algorithm on large integers. I guessed my only choice here was to bring the numbers to their ...

23. How do i compare values of BigInteger to be used as a condition in a loop?

I am trying to compare if the value of one BigInteger(base) is > the value of another BigInteger(prime) and if the value of 'a' is not equal to one. If ...

24. NumberFormatException, BigInteger in java

Getting following run-time error

C:\jdk1.6.0_07\bin>java euler/BigConCheck
Exception in thread "main" java.lang.NumberFormatException: For input string: "z
        at java.lang.NumberFormatException.forInputString(NumberFormatException.

25. Modulus not positive :BigInteger

error ---Modulus not positive BigInteger is taking vaule 0, or -ve, but i can't figure out where

public int[] conCheck(BigInteger big)
    int i=0,mul=1;
    int a[]= new int[10];

26. Java_stackoverflowerror using BigInteger.

The following code results in a stackoverflow error. How many, values of nCr, for 1<=n <=100, are greater than one-million? Please help.

import java.math.BigInteger;

public class combinatorics {

    private BigInteger ...

27. Logarithm for BigInteger

I have a BigInteger number, for example beyond 264. Now i want to calculate the logarithm of that BigInteger number, but BigInteger.log() is not present. How do I calculate it?

28. Why is this happening in BigInteger

BigInteger bx=new BigInteger("3806908688");

byte x[]=new byte[4];

String s=bx.toString(10);

System.out.println("string: "+s);


int l=0,i=0;

l |= x[i] & 0xFF;

l <<= 8;

l |= x[i+1] & 0xFF;

l <<= 8;

l |= x[i+2] & 0xFF;

l <<= 8;

l |= x[i+3] & 0xFF;

The output ...

29. Problem with a number when storing in BigInteger and retrieving back

BigInteger bx=new BigInteger("5888561920");


byte x[]=new byte[5];






        System.out.print(" "+t[i]);

System.out.println("\nbig: "+toInt(t));
The output for the above code is:

 94 -4 ...

30. Efficient BigInteger in Java

We have a set of locations within our product where BigInteger is required as the numbers can be fairly long. However, in over 90% of the cases, they are actually not ...

31. how to input a BigInteger type in java

when I tried to get an input of type Integer, what I only needed to do was the code below.

Scanner sc = new Scanner(;
int N = sc.nextInt();
but when it comes to ...

32. Why to avoid biginteger instantiation in Java

There is a PMD rule saying one should avoid to instantiate BigInteger or BigDecimal if there is a predefined constant.


// instead of

new BigInteger(0)
Would there be any other advantage ...

33. Java BigInteger Memory Optimization

I'm trying to find LCM of given N numbers. But this code of mine takes more than 32Mb memory. What kind of optimization can I perform here?

import java.util.Scanner ;
import java.math.BigInteger ; ...

34. Java How to invert a BigInteger?

I need to invert a BigInteger. Let's say i have BigInteger x; and i need to calculate x.modPow(new BigInteger("-1"), p). I receive the following error: java.lang.ArithmeticException: BigInteger ...

35. BigInteger exponents - how?

OK, well, here is some code: import java.math.BigInteger; public class BigIntegerX { public BigInteger pow(BigInteger mantissa, BigInteger exponent) { final BigInteger MAX_LONG = new BigInteger(Long.toString(Long.MAX_VALUE)); final BigInteger factor = new BigInteger(mantissa.toString()); BigInteger remainder = exponent.mod(MAX_LONG); for (int i = 0; i < remainder.longValue() - 1; i++) { mantissa = mantissa.multiply(factor); } exponent = exponent.subtract(remainder); while (exponent.compareTo(BigInteger.ZERO) > 0) { for (int ...

36. BigInteger back to text

Hi all, I am doing some Encryption type studies and have encrypted then decrypted ok( I think). I am struggling to get what was initially a simple text file converted to byte then BigInteger, back to the same presentable text. Can someone advise a process from BigInteger to a file save state. What I have tried with all sorts of variations ...

37. Advantage of BigInteger Class

The problem is that the methods in the Math class work with ints and doubles. Those types can hold only finite numbers. The BigInteger and BigDecimal classes can hold arbitrarily large numbers. Another advantage of BigDecimal is that because of the problem of representing decimal numbers in binary, doubles are not appropriate for financial programs. But BigDecimal gives you accuracy.

38. BigInteger in java

39. BigInteger Class Worked on my Hex-String

Hi guys, Thanks and sorry for posting one question twice because i want to change my question but it was posted so i didnot knew how i can edit. Well BigInteger worked my program though every Hex-String results as n Integer where i thought it might result as a Hex-String. Any Way i am happy that its working.... Regards.

40. BigInteger Power/Exponent BigInteger

isnt the code I wrote in the post after Campbell's suggestion does exactly what his code does but in java? because the code I wrote doesnt seem to work, in Campbell's code, he never decreases the exponent... or I cant understand the code correctly how to write a java code that does the same thing as Campbell's code does? can anyone ...

41. a little BigInteger problem

42. Biginteger

43. BigInteger

44. BigInteger and multiplying

45. BigInteger

46. Need help with BigInteger

47. Why no toBinaryString(BigInteger b)

Hi Varun, There is no toBinaryString() method, but there is a bitLength() method that tells you how many bits there are, and a testBit(int) method that tells you the state of each bit. So create a StringBuilder, store bitLength() in a variable "n", then in a for loop check each bit from n-1 to 0 and append the appropriate "1" or ...

48. Initial value for BigInteger from another BigInteger

You are right, I missed the "immutable". Suppose x is an array of BigInteger. I want to compute x[i] = (x[j] + x[k]) *x[n] for various values of j, k, and n that are less than i and functions of i. The value of the reference at x[i] will change several times as I compute the value. The garbage collector will ...

49. bigInteger to float or double

As soon as you turn it into a float you've lost quite a bit of accuracy already. That's because float only has 32 bits in which to store both the integer and the decimal part. double is better but still limited. The only way to maintain your accuracy is to keep using BigInteger.

50. BigInteger

51. BigInteger Operations

52. The bigInteger class

53. BigInteger

Those javadocs can be difficult to understand sometimes though. Basically, the BigInteger class was created through java to help mathematicians or anybody really, wanting to make calculations with numbers that use large amounts of digits in those numbers. Because when you initialise a variable such as a normal int or a byte or a double or float or a long etc, ...

54. My own BigInteger class. Need help.

55. very new... Biginteger problems

Hello I am very new to Java (this is my first java programming) and am puzzeling how to work with Big integers and i am not able to work it out My problems reduced to simple statements are the following Declaration I need to declare a new BigInteger with a value of 1 Multiply a named biginteger by 3 Add one ...

56. BigInteger value of

import java.math.*; import java.util.*; public class BigInteger { public static void main (String []args) { Scanner in = new Scanner(; System.out.println("How many numbers do you need to draw ?"); int k = in.nextInt(); System.out.print("What is the highest number you can draw ?"); int n = in.nextInt(); BigInteger lotteryodds = BigInteger.valueOf(1); for(int i = 1; i <=k; i++) lotteryodds = lotteryodds .multiply(BigInteger.valueOf(n ...

57. BigInteger Rational (CS106A-Stanford university)

BigInteger Rational (CS106A-Stanford university) Hi Folks, This is the exercise iam trying to solve from the book "Art and science of Java": The implementation of the Rational class given in this chapter is not particularly useful in practice because it doesnt allow the numerator and denominator to exceed the size of an integer, even though larger values tend to ...

58. BigInteger class in the art & science of java

Hi, I' m new to java. when I do the exercise in the book "the art and science of java", I come across a problem about BigInteger class. Here is question: The implementation of the Rational class given in this chapter is not particularly useful in practice because it doesnt allow the numerator and denominator to exceed the size of an ...

59. Does any one know how to write this e^sqrt(lognloglog(n)) ,where n is biginteger

If numerical accuracy is desired (I'm assuming it is), I'd write a log function using the BigInteger and BigDecimal classes directly. You might consider talking to a mathematician. This function appears to me to be monotonic, and so a gradient descent (Newton-Raphson) approach may work and provide a solution much faster than direct computation.

60. BigInteger

I have some code to add *very* large numbers. Java Code: import java.math.*; public class charles { public static void main(String[] args) { BigInteger a1 = new BigInteger("46376937677490009712648124896970078050417018260538"); BigInteger a2 = new BigInteger("74324986199524741059474233309513058123726617309629"); BigInteger a3 = new BigInteger("91942213363574161572522430563301811072406154908250"); BigInteger a4 = new BigInteger("23067588207539346171171980310421047513778063246676"); BigInteger a5 = new BigInteger("89261670696623633820136378418383684178734361726757"); BigInteger a6 = new BigInteger("28112879812849979408065481931592621691275889832738"); BigInteger a7 = new BigInteger("44274228917432520321923589422876796487670272189318"); BigInteger a8 = ...

61. Biginteger help

62. BigInteger

63. BigInteger

64. BigInteger Problem

You need to be more specific about what your problem is. Saying your gui doesn't work doesn't tell us anything useful. You shouldn't give your classes the same name as already existing classes, it just leads to confusion. There's already an Integer class in the api. If you do give them the same name, at least put it in a package ...

65. BigInteger modPow

66. BigInteger.probablePrime() doesn't output the correct bitlength?

From the API documentation for Random: "If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers." And: "Two Random objects created within the same millisecond will have the same sequence of random numbers." So you might consider using only one ...

67. What is BigInteger?

I tried reading the API and googling but couldn't really find anything that tell me what it is or I'm just not understanding it. So what's the difference between an int and a BigInteger? How many bits is a BigInteger? The closest thing I could find was "a BigInteger allows you to store an arbitrary big non-decimal number" . Can anyone ...

68. BigInteger multiply limit

YoungWinston wrote: I also noticed that your number is 4,093 digits long, which would suggest that the answer should be 4,097, so I wonder if some sort of 4K limit is being breached. But whether it's a BigInteger bug, or simply a problem with banging out that many characters to System.out in one go, I don't know. I did a little ...

69. how i can find log2 of BigInteger ?

70. Problem with BigInteger conversions

Hello, I have a question about the BigInteger class: How can I convert a string of decimal encoded bytes such as "12345678901234567890" to a BigInteger, then back to a string again? I want to translate it back to a string using toByteArray ( Yes I know it uses the two's compliment, I'm just not sure how to get it into a ...

71. DataType BigInteger

72. Problem with BigInteger and short type

I have a block of data in a byte array, and i must append at the beginning of this array those two bytes that represent the short value (this values is the amount of bytes in the block of data that is constant) i simply always have to have this zero value at the beginning, becouse the block of data must ...

73. BigInteger padding

so say I have: BigInteger big = new BigInteger("110001", 2); I want this to be "0110001", so I want to pad this with another 0 in front?? How do I do this? I tried shifting it to the right and it becomes 11000 instead of adding 0 in front of it... how do I do this?? may seem like a stupid ...

74. BigInteger class question

75. BigInteger heap space error in method add()

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.math.BigInteger.add(Unknown Source) at principal.crearCompuestosOpIguales( at principal.main( First, I thought that the problem was the amount of memory the JVM, but after bend, the problem persists. I have seen some errors in methods of the class BigInteger, in my case it gives in the method add, but I do not know how to ...

76. BigInteger Class

77. Extended Euclid BigInteger version

It would also help to learn how to debug, either stepping thru your code to see where it's doing something you didn't expect, or having it simply print intermediate values to cue you in to that. In this case you would have been able to see that the (q.compareTo(BigInteger.ZERO) == 1) wasn't doing what you thought it would do, because you ...

78. BigInteger problem ? Number of possible passwords with a given set

The code below gets the number of possible passwords created with a set of setLen and a starting password length of startNr and endNr as maximum length. Example: all number of passwords with letter from a to z and length from 1 to 8 will translate as passwdNr(27,1,8). This is computationally expensive: 2263 ms for a (70,1,9). Now the problem is ...

79. Problem with BigInteger class

80. Ln of BigInteger

81. Does BigInteger have a MaxValue limit?

I was just thinking , it takes at least 4 bits to represent a decimal digit (0 to 9) So that means 1 byte = 8 bits can hold 2 decimal digits. 8 GB = 8 x 1024 MB = 8 x 1024 x 1024 B = 8388608 B = 16777216 Digits What size memory does it take to hold 16777216 ...

82. Collision in BigInteger...

83. BigInteger Reporting incorrect values ???

if there are 24 characters in String then conversion should be 12 byte. This suggests that you have in mind a rule like "2 characters per byte". But you first turned the characters into a BigInteger. And BigIntegers (like other sorts of number) contains one extra piece of information that we are apt to forget sometimes when we write the number ...

84. how to use biginteger

85. Biginteger problem

this is my current code but it only works up to 20 digits because of the limitation on long for(int test = 0; test <20; test++){ if (temp.substring(i, topN).compareToIgnoreCase("0") == 0) myInt = BigInteger.valueOf(y + y*10+0); else if (temp.substring(i, topN).compareToIgnoreCase("1") == 0) y = y * 10 + 1; else if (temp.substring(i, topN).compareToIgnoreCase("2") == 0) y = y * 10 + ...

86. Is there a class for BigInteger in c++

87. BigInteger question

798089 wrote: I rarely comment on here because the last time I did, a poster was nasty to me, That would be this thread, which included this comment from me: I believe he's getting frustrated because you keep asking the same thing and keep getting told the same answer. If you don't understand the answer, then it's on you to expand ...

88. BigInteger comparision is failing in 1.5