简体   繁体   中英

Simplify square roots algebraically

I would like to simplify the square root of an integer algebraically, not compute it numerically, ie √800 should be 20√2 , not 28.2842712474619 .

I cannot find any way to solve this through programming:(

Factorize the number under the root, pick out the factors that come out in pairs and leave the rest under the root.

√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.

And for completeness, here some simple code:

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

when the algorithm terminates, outside_root and inside_root contain the answer.

Here the run with 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
     ==        ==

The answer 20√2 is here on the last row.

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

This could be one of the solution

I think this is efficient. I used it in my calculator app

I did that using java. Hope this will help

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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