系列 1*3-3*5+5*7 之和
请帮助打印系列以及系列总和,例如 1*3-3*5+5*7 最多 n 个术语 我已经在 php 中使用了这样的代码
class series {
function ser(){
$i = 0;
$k = 3;
$m = 0;
for($j = 1; $j < 3; $j++) {
if($j % 2 == 0 ) {
$m = $i + ($i * $k);
} else {
$m=$m-($i*$k);
}
}
//$m = $m + $i;
echo "$m";
}
}
$se = new series();
$se->ser();
我已经测试了 2 次
please help to print series as well sum of series like 1*3-3*5+5*7 up to n terms i have used code like this in php
class series {
function ser(){
$i = 0;
$k = 3;
$m = 0;
for($j = 1; $j < 3; $j++) {
if($j % 2 == 0 ) {
$m = $i + ($i * $k);
} else {
$m=$m-($i*$k);
}
}
//$m = $m + $i;
echo "$m";
}
}
$se = new series();
$se->ser();
Just i have tested for 2 times
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
通过一些简单的运算,我们可以找到总和 S 的公式。
如果 n 为偶数(总和 Se),则将各项对相加得出
括号中的项可以拆分并求和:
如果 n 为奇数(总和 So),则必须将最后一项添加到 Se:
C 中的实现:
With a few simple operations one can find a formula for the sum S.
If n is even (sum Se) adding pairs of terms yields
The terms in the parenthesis can be splitted and summed up:
If n is odd (sum So) the last term must be added to Se:
The implementation in C:
这可能是家庭作业,但无论如何。希望你能从中学到一些东西。
上面的代码太可怕了。太复杂了……这是给您的一个非常简单的版本。我不知道这是用什么语言写的,但我会为你做类似的事情......去买一本关于编程的书,这将是你时间的明智投资。
希望这会有所帮助...您可能会明白这一点。
This is probably homework, but here goes anyway. Hopefully you learn something from this.
The code above is horrible. Over-complicated for nothing... Here's a very simple version for you. I have no idea of what language this is in, but I'll do something similar for you... Go get a book on programming, that will be a wise investment of your time.
Hope this helps... You probably get the idea with this.
我更喜欢递归函数,通过这种方式你可以 stackoverflow (woot!) :) :
I prefer the recursive function and by this way you can stackoverflow (woot!) :) :
或者,使用以下公式计算级数的前 n 项。抱歉,我还没弄清楚如何让 SO 正确显示 LaTeX,也许有人可以帮我编辑它,但如果你这样做,请留下评论并附上说明!
\frac{1}{2} \left(-4 (-1)^nn^2-4 (-1)^n
n+(-1)^n-1\right)
或者,由精彩的 EquationSheet.com:
(来源:equationsheet.com)
Or, use the following to compute the first n terms of your series. Sorry haven't figured out how to make SO display LaTeX properly, perhaps someone can edit it for me, but if you do please leave a comment with instructions please !
\frac{1}{2} \left(-4 (-1)^n n^2-4 (-1)^n
n+(-1)^n-1\right)
Or, as generated by the wonderful EquationSheet.com:
(source: equationsheet.com)