当数字是分数时,使用 Java 找到数字的完美 p 次幂

如果 x 可以表示为另一个数 b^p,我们就说 p 是一个数 x 的完美 p 次幂。


即如果 x=b^p ,则 p 是 x 的完美 p 次方。


我很少有 x 可以是正整数、负整数甚至分数的用例。前两种情况在 java 中很容易处理,但是当 x 是分数时,如何使用 java 找出数字 x 的完美 p 次方。如果 x 是一个分数,我们可以简单地使用 Math.sqrt(x) 并得到一个数字 b 使得 b^2 =x 是不是真的?那么 2 将是 x 的完美 p 次方。这个案例是否有效?


我不一定在寻找代码,而是在 x 是分数时确定 java 中 x 的完美 p 次幂的逻辑。如果有人认为此案例无效,也请说明您的理由。


下面是我编写的代码,用于处理 x 是正整数或 0 到 1 之间的数字的情况。但是我们可以处理 x 是的情况,例如。45.487,875515.54884,等等?


public class PerfectPower {


public PerfectPower() {


    }


    public Integer getPerfectPower(double x){


        // x=b^p


        int p = 0;

        double b;


        if(x==0){

            throw new IllegalArgumentException("Cannot accept number 0.");

        }


        if (x > 1) {

            for (b = 2; b <= x; b++) {


                double value = 0;

                p = 1;


                while (value <= x) {


                    value = Math.pow(b, p);


                    if (value == x) {

                        return p;

                    } else if (value > x) {

                        break;

                    } else {

                        p++;

                    }

                }


            }

        } else if(x>0 && x<1){


            for (b = 2; (1/b) >= x; b++) {


                double value = 1;

                p = -1;


                while (value >= x) {


                    value = Math.pow(b, p);


                    if (value == x) {

                        return p;

                    } else if (value < x) {

                        break;

                    } else {

                        p--;

                    }

                }


            }


        }


        return null;


    }


慕桂英546537
浏览 181回答 2
2回答

holdtom

我们可以使用对数以简单的方式做到这一点。它如下:x = b^p&nbsp; &nbsp; log(base b)x = p&nbsp; &nbsp; &nbsp;log x/log b = p&nbsp;&nbsp;所以我们可以通过 x 迭代 b = 2 并检查 p 是否是一个完美的整数并返回值。对于十进制情况,我们可以进一步调整日志公式。log b = (log x)/p&nbsp; &nbsp; Hence b = 10^(log x)/p)&nbsp;在每次迭代中,我们可以检查是否 b^p = x 以及是否返回 p。我解决了这个问题,假设 p 应该是一个整数。然而,对于 p 可以是十进制而 x 在 0 到 1 之间的情况,这个解决方案应该进一步调整。下面是我在 Scala 中实现的代码。def perfectpowerlog(x: Double): Double = {&nbsp; &nbsp; var i: Double = 2&nbsp; &nbsp; var n: Double = 1&nbsp; &nbsp; var p: Double = 0&nbsp; &nbsp; val loop = new Breaks&nbsp; &nbsp; if (x == 1) {&nbsp; &nbsp; &nbsp; &nbsp; return n&nbsp; &nbsp; }&nbsp; &nbsp; if (x.ceil == x) {&nbsp; &nbsp; &nbsp; &nbsp; loop.breakable {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (i<=x) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; p = math.log(x)/math.log(i)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (p.toInt == p) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; n = p&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; loop.break()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i=i+1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; loop.breakable {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while(i<=x.ceil) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; p = pow(10,(log10(x)/i))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(pow(p,i) == x) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; n = i&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; loop.break()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i = i+1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return n}

哔哔one

由于 45 487.875 515 548 84(以此为例)不是整数,所以它可以表示为 b ^ p 其中 b 和 p 是整数的唯一方法是如果 p 是负数。也就是说,您的数字可能是某个(大)整数的平方根、立方根、四次根等。第一个问题是精度问题。您的数字无法用 Java 双精度精确表示。您可以使用BigDecimal.&nbsp;它也不能恰好是某个整数的某个根,因此您必须决定接受的容差。据我所知,你的大问题是可能的 p 值的范围是无限的。甚至可能所有数字都足够接近某个(大)整数的第 p 个根,而您无法合理区分;我不知道,这肯定取决于你的容忍度。我认为您可以尝试的最好方法是将您的数字提高到 2、3、4 等,然后看看您何时接近整数。如果您的 q 次幂足够接近整数,则返回 -q 作为您的 p。在你失去耐心之前停止搜索。:-)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java