本帖最后由 0什么都不懂 于 2021-1-13 17:03 编辑 程序如下: n = 123456; format short; cube = 0; sum = 0; for i = 1:n cube = cube + i^3; sum = sum + i; end y = (cube*2 - sum^2)/(n/2); y = uint64(y); 结果:y = 940835389047074 给出的结果是:y = 940835389047072 |
最佳答案
7 条回复
机器精度的缘故,当数值大到一定程度后,存储的时候就会有误差了,对待大数运算需要特别谨慎,该用符号型变量就用符号型
y = 940835389047072 |
能具体说一下你的题目要求吗,只有立方和与平方和? |
hyh0lh 发表于 2021-1-13 18:11 Given the positive integers 1:n, can you: 1. Compute twice the sum of the cubes of those numbers. 2. Subtract the square of the sum of those numbers. 3. Divide that result by n/2. |
TouAkira 发表于 2021-1-13 17:59 那具体该怎么修改呢? |
已经有高手给你正确答案了:sym |
hyh0lh 发表于 2021-1-13 18:36 好的,谢谢啦 |
Powered by Discuz! X3.4
© 2001-2021