1

Any chance you might be able to think of an efficient way to split a BigInteger in half, i.e. if the number is 12345678, it would be split into 1234? I was thinking that I could change it into a string and use substring, but I'm not sure if that would be the fastest way to do it.

Do you also know how to count the number of digits in BigInteger? I know that you can do a bitLength and bitCount, but I think those are in two's complements. I'm trying to keep track of where I split them?

Prince John Wesley
  • 62,492
  • 12
  • 87
  • 94
user1020415
  • 33
  • 2
  • 5
  • How critical is speed to you? If its really critical I wouldn't use BigInteger, just use a long. I suspect simplicity is all you need. – Peter Lawrey Nov 02 '11 at 08:20
  • Check this:- http://stackoverflow.com/questions/18828377/biginteger-count-the-number-of-decimal-digits-in-a-scalable-method – Avneesh Sep 07 '14 at 12:20

3 Answers3

3
    BigInteger bi = new BigInteger("12345678");
    String numStr = bi.toString();
    System.out.println(numStr.substring(0,numStr.length()/2));
Prince John Wesley
  • 62,492
  • 12
  • 87
  • 94
1

Some asked how to calculate the number of decimal digits in a binary number here: https://math.stackexchange.com/questions/3987/how-to-calculate-the-number-of-decimal-digits-for-a-binary-number

Seems like its not trivial. So it is very likely that you will have to convert to a String and work from there.

Community
  • 1
  • 1
David Tinker
  • 9,383
  • 9
  • 66
  • 98
1

Converting to String is probably your best bet because it sounds like you're looking for the 'textual' half point and length, which is not something that 'numerical' representations provide.

pra9ma
  • 388
  • 1
  • 4