Simplify square roots algebraically
-
06-06-2021 - |
質問
I would like to simplify the square root of an integer algebraically, not compute it numerically, i.e. √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) = √(22 x 22 x 52 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;
}
所属していません StackOverflow