-1

I want find the positive integer that, when taking the square of it, has the form 1_2_3_4_5_6_7_8_9_0 ("_" = random number). I wrote this code:

BigInteger num = new BigInteger("1010000000");
        while (String.valueOf(num).length() <= 19) {
            if (String.valueOf(num.pow(2)).length() < 19) {
                num = num.add(BigInteger.ONE);
            } else {
                if (check(num)) {
                    System.out.println("Answer: " + num);       
                    break;
                } else {
                    num = num.add(BigInteger.ONE);              
                }
            }           
        }           
    }

    public static boolean check(BigInteger contNum) {
        boolean control = false;

        String temp = String.valueOf((contNum.pow(2)));
        String con = "";

        for (int i = 0; i < 19; i = i + 2) {
            con += temp.substring(i, i + 1);
        }
        if (con.equals("1234567890")) {
            control = true;

        }
        System.out.println("con: " + con);
        return control;
    } 

It does, however, never reach the form of 1234567890 (The string I extract from the original number). What is wrong with this code? Thanks.

Other version:

long num = 1099999999;
        while (true) {
            if (String.valueOf(Math.pow(num, 2)).length() < 19) {
                System.out.println(String.valueOf(Math.pow(num, 2)).length());
                num++;
                System.out.println("Loading..");
            } else if (String.valueOf(Math.pow(num, 2)).length() > 19) {
                System.out.println(String.valueOf(Math.pow(num, 2)).length());
                System.out.println("Not found!");
                System.exit(0);
            } else {
                if (check(num)) {
                    System.out.println("Answer: " + num);       
                    break;
                } else {
                    num++;          
                }
            }

        }           
    }

    public static boolean check(long contNum) {
        boolean control = false;

        String temp = String.valueOf(Math.pow(contNum, 2));
        String con = "";

        for (int i = 0; i < 19; i = i + 2) {
            con += temp.substring(i, i + 1);
        }
        if (con.equals("1234567890")) {
            control = true;         
        }
        System.out.println("t: " + temp);
        return control;
    } 

3 Answers3

1

I don't see anything wrong with your code per se, except that it is extremely slow. You will get the answer only after a very long time.

Here's some tips on how to speed it up:

  • Since the last digit is 0, it follows that the original number must be a multiple of 10, so you can increment by 10 each time.
  • The min value is 1020304050607080900 and the max value is 1929394959697989990. These fit within a long, so you should use long instead of BigInteger as it is much faster. Likewise, you can run your loop only between the square roots of the min and max values.
  • Using a String to check the answer is inefficient. Instead consider checking the last digit repeatedly by taking the remainder mod 10, then dividing by 100.
nitegazer2003
  • 1,193
  • 5
  • 10
  • in fact for a square to end with 0, it has to have the last 2 digits as zeros, so the max squared value is 1929394959697989900 – diginoise Mar 08 '16 at 09:19
  • Hello @nitegazer2003 I changed my code (see "other version" in my post). Now, I have trouble with the length of the number. Why is that when I change it to long? Thanks! –  Mar 08 '16 at 09:31
  • @nitegazer2003 it gets to length "22" when it should be 19. How come? –  Mar 08 '16 at 10:08
  • @Aminorph Math.pow returns a double, so most likely there are decimal points. Since its a long, you can simply check whether num <= max, where max is the number you want to stop at. – nitegazer2003 Mar 08 '16 at 10:10
  • @nitgazer2003 Thank you! –  Mar 08 '16 at 10:14
1

Try this.

    int[] base = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
    int length = base.length;
    for (int i = 0; i <= 999999999; ++i) {
        long v = base[0];
        for (int k = 1, n = i; k < length; ++k, n /= 10) {
            v *= 10; v += n % 10;
            v *= 10; v += base[k];
        }
        if (isPerfectSquare(v))
            System.out.println(v);
    }
    // -> 1929374254627488900

Here is boolean isPerfectSquare(long n)

Community
  • 1
  • 1
0

Your code works I checked it out only it takes a lot of time to give the correct result and when I ran it in my machine it also took a lot of time.Here is the number int the below which got me the result.

public class Test {

    public static void main(String[] args) {

        BigInteger num = new BigInteger("1389019170");
        while (String.valueOf(num).length() <= 19) {
            if (String.valueOf(num.pow(2)).length() < 19) {
                num = num.add(BigInteger.ONE);
            } else {
                if (check(num)) {
                    System.out.println("Answer: " + num);
                    break;
                } else {
                    num = num.add(BigInteger.ONE);
                }
            }
        }

    }

    public static boolean check(BigInteger contNum) {
        boolean control = false;

        String temp = String.valueOf((contNum.pow(2)));
        String con = "";

        for (int i = 0; i < 19; i += 2) {
            con += temp.substring(i, i + 1);

        }
        if (con.equals("1234567890")) {
            control = true;
        } else {
        }
        System.out.println("con: " + con);
        return control;
    }
}