0

I'm looping through BigInteger like this:

BigInteger i = new BigInteger(Long.MAX_VALUE + "");
    BigInteger end = i.add(new BigInteger("10"));

    // Display results
    for (i = new BigInteger(Long.MAX_VALUE + "");
         i.compareTo(end) <= 0;
         i = i.add(new BigInteger("1"))) {
        System.out.println(i.multiply(i));
    }

And that's too expensive due memory usage, because of creating new BigInteger object every loop instead of just increment the value using ++.

Is there a way to increment the value instead of creating new object every time?

  • 2
    I'm sensing a possible [XY Problem](http://xyproblem.info/) here where the best solution is to use an approach that is completely different from what you are trying to do. Consider telling us the overall problem that you're trying to solve rather than how you're currently trying to solve it. – Hovercraft Full Of Eels Nov 20 '17 at 02:27
  • 2
    While `int` is limited to `Integer.AX_VALUE` (2^31-1), if you want to loop over a wider range you can nest two for-loops (or more). That said, I'm curious what's your use-case because I never ran into such a weird requirement... – Nir Alfasi Nov 20 '17 at 02:28
  • @HovercraftFullOfEels that's right I dun know how to go through this, I just tried normal way as it's normal ìnt` and of course fail. –  Nov 20 '17 at 02:31
  • Why do you say it's too expensive? Is your JVM running out of memory? – Dawood ibn Kareem Nov 20 '17 at 02:33
  • @DawoodibnKareem as far as I know, objects added to stack, that's mean more objects in stack required more memory, is I'm right or am missing something here? –  Nov 20 '17 at 02:37
  • @alfasin `int` is limited and `Integer.MAX_VALUE` is limited too comparing with `BigInteger`. –  Nov 20 '17 at 02:43
  • 1
    Yeah, I think you've misunderstood what the stack is. I don't see anything wrong with your code as it is. I really don't expect you'll have a problem with memory usage, and I find it strange that you're asserting that this code is too expensive. Sure, it can be improved slightly, as per Elliott's answer, but it's not going to make a huge difference. – Dawood ibn Kareem Nov 20 '17 at 02:49
  • More importantly, if this code is part of an application that _does_ have a memory usage problem, then the problem is _not_ in this code - it's somewhere else. Using Elliott's code will save you a handful of bytes - it's true. But your computer has a few billion bytes to play with. Worrying about this is rather like trying to shrink the national debt by buying cheaper coffee for the president. – Dawood ibn Kareem Nov 20 '17 at 02:54
  • @DawoodibnKareem you're right thanks, but I'm using this block a lot in my app and Elliott's answer is 1x faster than above code, since i'm doing long big operations then it will be some helpful. –  Nov 20 '17 at 02:56
  • Then why not use `long` directly? what's the added value of using BigInteger ? and by the way, when you "answered" me you simply repeated some of the things I wrote, this is not a proper response, please read again my initial comment. – Nir Alfasi Nov 20 '17 at 03:05

1 Answers1

1

Well, you can use BigInteger.valueOf(long) to avoid parsing a String. And you can use BigInteger.ONE instead of re-creating a 1 with every iteration. Like,

BigInteger end = BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.valueOf(10));
// Display results
for (BigInteger i = BigInteger.valueOf(Long.MAX_VALUE); i.compareTo(end) <= 0; 
            i = i.add(BigInteger.ONE)) {
    System.out.println(i.multiply(i));
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249