如何测试一个数的平方根是否是有理数?

13

如何测试一个数的平方根是否为有理数?

这真的可能吗?

我需要知道这个原因是因为我正在开发一款数学应用程序,需要确定是否将数字显示为整根式。


这可能更适合在math.stackexchange.com上问。他们会给你算法,然后你可以在这里实现它。 - Dheer
2
@Dheer 为了证明他实际上正在寻找完全平方数,是的;但是对于测试的计算方法,这是正确的地方。 毕竟,对于数学家来说,“n是完全平方数吗?”的答案只是“当且仅当sqrt(n)是整数”,这在程序上并没有太大帮助。 - AakashM
什么是数字?整数,有理数还是浮点数? - jk.
浮点数/双精度浮点数(数据类型)但很可能是整数(不是数据类型)。 - Alex Coplan
@sarnold,的确,我甚至没有考虑过非整数。严谨地说,我在评论中使用“n”显然意味着我正在谈论一个整数 :) - AakashM
5个回答

6

3
你忘记了平方根不仅可以应用于整数,也可以应用于任何数。引用维基百科的话:若 x 为一个可以表示为两个完全平方数之比的有理数,则√x是有理数。 - sarnold
所有有限浮点数都是有理数。不需要进行任何检查。 - Stephen Canon
@StephenCanon:但是每个数都代表着无限数量的有理数和无理数,它们都具有相同的有限浮点表示。 - thiton
@thiton:不,每个数字都精确地代表一个有理数。其他数字会被四舍五入到最接近的可表示值。你会说整数3“代表”3.2吗?因为如果我写int x = 3.2,实际上我得到的是x == 3 - Stephen Canon

3

来自维基百科的内容:如果x是一个可以表示为两个完全平方数比值的有理数,那么x的平方根也是有理数。

因此,您需要找到输入数字的有理近似值。到目前为止,我掌握的唯一算法是为HP48系列计算器编写的Saturn汇编语言。


2

阅读评论和另一个问题的答案后,我意识到问题源自浮点数不准确性,这意味着在程序末尾,某些值(例如0.01)会通不过逻辑测试。我已将其改为使用NSDecimalNumber变量。

double num, originalnum, multiplier;
int a;

NSLog(@"Enter a number");
scanf("%lf", &num);
//keep a copy of the original number
originalnum = num;

//increases the number until it is an integer, and stores the amount of times it does it in a
for (a=1; fmod(num, 1) != 0 ; a++) {
    num *= 10;
}

a--;
//when square-rooted the decimal points have to be added back in
multiplier = pow(10, (a/2));
if (fmod(originalnum, 1) != 0) {
    multiplier = 10;
}

NSDecimalNumber *temp = [NSDecimalNumber decimalNumberWithDecimal:[[NSNumber numberWithDouble:sqrt(num)/multiplier] decimalValue]];
NSDecimalNumber *result = [temp decimalNumberByMultiplyingBy:temp];
NSDecimalNumber *originum = [NSDecimalNumber decimalNumberWithDecimal:[[NSNumber numberWithDouble:originalnum] decimalValue]];

if ((fmod(sqrt(num), 1) == 0) && ([result isEqualToNumber:originum])) {
    NSLog(@"The square root of %g is %@", originalnum, temp);
}
else {
    NSLog(@"The square root of this number is irrational");
}

-1 用 1f/3f 平方、0.1f 平方、0.101f 平方测试此算法会得到“该数的平方根为无理数”的结果。你需要将分子和分母精确表示为整数,并测试它们是否为精确平方数。 - Pete Kirkham

1

如果你正在处理整数,需要注意的是,正整数有一个有理平方根当且仅当它有一个整数平方根,也就是说,如果它是一个完全平方数。关于如何测试这一点,请参见这个 StackOverflow 问题


0

https://math.stackexchange.com/上,有一个问题什么有理数有有理数平方根?,得到了Jakube的答案,其中指出对于“...有理数,一个答案是确定分子和分母是否为2的整数次幂。”

确定自然数是否为完全平方数的好方法取决于函数支持的自然数(以及使用的计算机编程语言)和可用的内存等。以下是一组有用的链接:

我使用Java开发并测试了一个解决方案,它对一组自然数运行良好。以下是其要点。此代码依赖于BigMath,并在agdt-java-math中实现,尽管在几个不同的类中:

    /**
     * @param x The number to return the square root of (if the result is
     * rational).
     * @return The square root of x if that square root is rational and
     * {@code null} otherwise.
     */
    public static BigRational getSqrtRational(BigRational x) {
        BigInteger[] numden = getNumeratorAndDenominator(x);
        BigInteger nums = getPerfectSquareRoot(numden[0]);
        if (nums != null) {
            BigInteger dens = getPerfectSquareRoot(numden[1]);
            if (dens != null) {
                return BigRational.valueOf(nums).divide(BigRational.valueOf(dens));
            }
        }
        return null;
    }

    /**
     * @param x The value for which the numerator and denominator are returned.
     * @return The numerator and denominator of {@code x}
     */
    public static BigInteger[] getNumeratorAndDenominator(BigRational x) {
        BigInteger[] r = new BigInteger[2];
        r[0] = x.getNumeratorBigInteger();
        r[1] = x.getDenominatorBigInteger();
        if (Math_BigInteger.isDivisibleBy(r[0], r[1])) {
            r[0] = r[0].divide(r[1]);
            r[1] = BigInteger.ONE;
        }
        return r;
    }

    /**
     * @param x The number to return the square root of if that is an integer.
     * @return The square root of {@code x} if it is an integer and {@code null}
     * otherwise.
     */
    public static BigInteger getPerfectSquareRoot(BigInteger x) {
        if (x.compareTo(BigInteger.ZERO) != 1) {
            return null;
        }
        BigInteger xs = x.sqrt();
        if (x.compareTo(xs.multiply(xs)) == 0) {
            return xs;
        }
        return null;
    }

由于任何有理数的平方都是有理数,因此没有任何有理数是无理数的平方根。在阅读了Yves answer to: Prove that the square root of any irrational number is irrational之后,我对此非常清楚。因此,只需处理有理数的情况即可回答所有实数的问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接