解决方法
对根数下的数字进行分解,选出成对出现的因子,将其余部分留在根下.
√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.
为了完整起见,这里有一些简单的代码:
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在最后一行.