12 Replies Latest reply: Feb 6, 2007 4:14 PM by 807606 RSS

    Biginteger problem

    807606
      I am reading a text file in as a string, and need to compare 50 digits at a time to see if there prime. My issue is how do I get subtypes of string into a bigint?
        • 1. Re: Biginteger problem
          807606
          subtypes of string
          ??

          do you mean substrings?
          • 2. Re: Biginteger problem
            807606
            yes i did mean substring, my issue now is getting two big ints together like if I have big int 2 and big int 7 how do i have big int 27?
            • 3. Re: Biginteger problem
              807606
              You want to concatenate the numbers as if they were strings?

              I'd get the String representations of each, then concat the Strings, and create a new BigInteger from that String result. I haven't used BigIntegers much, so there's probably a better way to do it.
              • 4. Re: Biginteger problem
                807606
                the problem is long is not big enough to hold 50 digits
                • 5. Re: Biginteger problem
                  807606
                  the problem is long is not big enough to hold 50
                  digits
                  Which I take it is why you're using BigInteger in the first place. I don't get what you're asking here.
                  • 6. Re: Biginteger problem
                    807606
                    ??
                    BigInteger two = new BigInteger("2");
                    BigInteger seven = new BigInteger("7");
                    BigInteger twentySeven = new BigInteger( two.toString().concat( seven.toString() ) );
                    • 7. Re: Biginteger problem
                      807606
                      how to get long values from a txt file into my one bigInt variable without earasing the previous number. so if 2 goes into my big int variable and i want to add 7 to my big int variable how do I do it so my big int variable is 27
                      • 8. Re: Biginteger problem
                        807606
                        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 + 2;
                        else if (temp.substring(i, topN).compareToIgnoreCase("3") == 0)
                        y = y * 10 + 3;
                        else if (temp.substring(i, topN).compareToIgnoreCase("4") == 0)
                        y = y * 10 + 4;
                        else if (temp.substring(i, topN).compareToIgnoreCase("5") == 0)
                        y = y * 10 + 5;
                        else if (temp.substring(i, topN).compareToIgnoreCase("6") == 0)
                        y = y * 10 + 6;
                        else if (temp.substring(i, topN).compareToIgnoreCase("7") == 0)
                        y = y * 10 + 7;
                        else if (temp.substring(i, topN).compareToIgnoreCase("8") == 0)
                        y = y * 10 + 8;
                        else if (temp.substring(i, topN).compareToIgnoreCase("9") == 0)
                        y = y * 10 + 9;
                        i++;
                        topN++;
                        myInt = BigInteger.valueOf(y);
                        • 9. Re: Biginteger problem
                          807606
                          import java.math.*;
                          
                          public class Test {
                              public static void main(String[] args) {
                                  String number = "31415926535897933141592653589793";
                                  BigInteger big = new BigInteger(number);
                                  System.out.println("Is probable prime = " + big.isProbablePrime(10));
                              }
                          }
                          Don't code your input as an int or long but as a string.
                          • 10. Re: Biginteger problem
                            807606
                            Try this:
                            myInt = BigInteger.valueOf( y*10 + new BigInteger( temp.substring(i, topN) ) );
                            Furthermore, I'm sure there is a way not to have to do this one digit at a time.
                            • 11. Re: Biginteger problem
                              807606
                              you cannot have operators in the value of method such as + or *
                              • 12. Re: Biginteger problem
                                807606
                                All right, you made this my mission :).
                                import java.math.BigInteger;
                                
                                
                                class test {
                                
                                     public static void main(String [] args) {
                                          BigInteger two = new BigInteger("2");
                                          String seven = "7";
                                          BigInteger ten = new BigInteger("10");
                                
                                          // String seven = temp.substring(i, topN);
                                          BigInteger twentySeven = two.multiply( ten ).add( new BigInteger( seven ) );
                                          System.out.println(twentySeven);
                                     }
                                }