Java查找特定整数的平方

时间:2016-03-08 08:35:56

标签: java

我想找到正整数,当取正方形时,其形式为1_2_3_4_5_6_7_8_9_0(“_”=随机数)。我写了这段代码:

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;
    } 
但是,它确实永远不会达到1234567890的形式(我从原始数字中提取的字符串)。这段代码有什么问题?感谢。

其他版本:

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 个答案:

答案 0 :(得分:1)

我认为你的代码本身没有任何问题,只是它非常慢。你会在很长一段时间后得到答案。

以下是有关如何加快速度的一些提示:

  • 由于最后一位数字为0,因此原始数字必须是10的倍数,因此每次都可以增加10。
  • 最小值为1020304050607080900,最大值为1929394959697989990.这些值适合long,因此您应该使用long而不是BigInteger,因为它更快。同样,您只能在最小值和最大值的平方根之间运行循环。
  • 使用字符串检查答案效率低下。而是考虑通过取余数mod 10,然后除以100来重复检查最后一位数。

答案 1 :(得分:1)

试试这个。

    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

Hereboolean isPerfectSquare(long n)

答案 2 :(得分:0)

你的代码工作我检查了它只需要花费很多时间来给出正确的结果,当我在我的机器上运行时它也花费了很多时间。这是下面的数字int得到了我的结果

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;
    }
}