簡體   English   中英

Java找到特定整數的平方

[英]Java Find a the square of specific integer

我想找到一個正整數,取其平方時的形式為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;
    } 

我看不到您的代碼本身有什么問題,除了它非常慢。 很長一段時間后,您才能得到答案。

以下是有關如何加快速度的提示:

  • 由於最后一位為0,因此原始數字必須為10的倍數,因此每次可以增加10。
  • 最小值是1020304050607080900,最大值是1929394959697989990。這些值適合在long ,因此應使用long代替BigInteger,因為它更快。 同樣,您只能在最小值和最大值的平方根之間運行循環。
  • 使用字符串檢查答案效率很低。 取而代之的是考慮取余數mod 10,然后除以100,反復檢查最后一位數字。

嘗試這個。

    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

boolean isPerfectSquare(long n)

您的代碼可以正常工作,我檢查出來的結果只是花了很多時間才能給出正確的結果,而當我在計算機上運行它時也花費了很多時間。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM