c – 以代数方式简化平方根

c – 以代数方式简化平方根,第1张

概述我想用代数方式简化整数的平方根,而不是用数字计算它,即√800应该是20√2,而不是28.2​​842712474619. 我找不到任何方法通过编程来解决这个问题:( 对根数下的数字进行分解,选出成对出现的因子,将其余部分留在根下. √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. 为了完整起见,这 我想用代数方式简化整数的平方根,而不是用数字计算它,即√800应该是20√2,而不是28.2​​842712474619.

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

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

√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 = 1insIDe_root = 800d = 2while (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在最后一行.

总结

以上是内存溢出为你收集整理的c – 以代数方式简化平方根全部内容,希望文章能够帮你解决c – 以代数方式简化平方根所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/langs/1236806.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存