0

In Java how can I get a subsequence of BigInteger whose indices can be 10^5 digits long.

Example : BigInteger length is 10^5 . I have to find subsequence between index 10^3 and 10^4

khetanrajesh
  • 300
  • 3
  • 13
  • 1
    Can you please show us what have you tried so far? – Pritam Banerjee Jul 29 '16 at 22:47
  • Have you tried the `substring(int, int)` method? – RamenChef Jul 29 '16 at 22:48
  • Yes , it does not work as substring takes int as arguments , but in my case they are bigIntegers – khetanrajesh Jul 29 '16 at 22:50
  • `new String(new char[100000]).substring(1000, 10000)` – Andreas Jul 29 '16 at 22:52
  • 3
    @user2418640 A `String` is backed by a `char[]`, and an array cannot have more than `Integer.MAX_VALUE` elements, so indexes cannot exceed the range of `int`, ergo there is no need for index to be `BigInteger`. – Andreas Jul 29 '16 at 22:55
  • @Andreas you are correct . I have update the question . Please check now . – khetanrajesh Jul 29 '16 at 23:15
  • A `BigInteger` doesn't have "indices" or "subsequences", so it's unclear what it is you want. Are you saying that if the `BigInteger` has the value `12345678` and you want subsequence between index 1 (inclusive) and index 4 (exclusive), that means a new number with value `567`? or `234`? or what? – Andreas Jul 29 '16 at 23:42
  • @Andreas Yes you are correct. – khetanrajesh Jul 30 '16 at 08:02
  • I like being correct, but since I listed 3 mutually exclusive choices, I still don't know what I was correct about. Update your question and clarify what it is you wanted, e.g. with an example like I gave. – Andreas Jul 30 '16 at 15:36

2 Answers2

1

You can use:

String yourSubstring = Str.substring(9999, 10000);

This is will give you the string from 10^3 to 10^4.

To convert a BigInt to String you can use:

String str = yourBigInt.toString();
Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
0

Study this code and see if it solves your question. If it doesn't, maybe you will find some information within that will help.

public class TestBigInteger 
{
    public static void main( String[] args )
    {
        String makeNumber = "";
        int numberOfDigits = 10000;
        String newNumberString = "";
        Random random = new Random();
        BigInteger result;


        for ( int i = 0; i < numberOfDigits; i++ ) {
            makeNumber += random.nextInt( 9 );
        }
        newNumberString = makeNumber.substring( 100, 999);
        result = new BigInteger( newNumberString );

    }

}