繁体   English   中英

以代数方式简化平方根

[英]Simplify square roots algebraically

我想以代数方式简化 integer 的平方根,而不是用数值计算,即√800应该是20√2 ,而不是28.2842712474619

我找不到通过编程解决这个问题的任何方法:(

对根数下的数字进行分解,选出成对出现的因子,将其余部分留在根下。

√800=√(2 x 2 x 2 x 2 x 5 x 2 x 5)=√(2 2 x 2 2 x 5 2 x 2)=(2 x 2 x 5)√2=20√2。

为了完整起见,这里有一些简单的代码:

outside_root = 1
inside_root = 800
d = 2
while (d * d <= inside_root):
  if (inside_root % (d * d) == 0): # inside_root evenly divisible by d * d
    inside_root = inside_root / (d * d)
    outside_root = outside_root * d
  else:
    d = d + 1

当算法终止时,outside_root和inside_root包含答案。

这里运行800:

 inside   outside   d
    800         1   2 # values at beginning of 'while (...)'
    200         2   2
     50         4   2
     50         4   3
     50         4   4
     50         4   5
      2        20   5 # d*d > 2 so algorithm terminates
     ==        ==

答案20√2在最后一行。

#include<stdio.h>
#include<conio.h>
int main() {
    int i, n, n2, last, final;
    last = 0, final = 1;
    printf("Enter number to calculate root: ");
    scanf("%d", & n);
    n2 = n;
    for (i = 2; i <= n; ++i) {
        if (n % i == 0) {
            if (i == last) {
                final = final * last;
                last = 0;
            } else {
                last = i;
            }
            n /= i;
            i--;
        }
    }
    n = n2 / (final * final);
    printf("\nRoot: (%d)^2 * %d", final, n);
    getch();
    return 0;
}

这可能是解决方案之一

我认为这是有效的。 我在我的计算器应用程序中使用它

我使用 java 做到了这一点。 希望这会有所帮助

  static void surd_form(long a) {
    long i = 2;
    long sq = 1;
    long k = 4;
    long p = 1;
    while (k <= a) {
        if (a % i == 0) {
            if (a % k == 0) {
                a /= k;
                sq *= i;
            } else {
                a /= i;
                p *= i;
            }
        } else {
            i += 1;
        }
        k = i * i;
    }
    System.out.println(sq + "" + "√" + (a * p));
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM