Heres the series:
以下是系列:
Note that, when the denominator is even, it adds up to the sum but when the denominator is odd, it first subtracts from the sum then again for the next term, it adds to the sum. For every consecutive term, the power of the numerator will get updated by +2.
请注意,当分母是偶数时,它加起来就是和,但当分母是奇数时,它首先从总和中减去,然后在下一项中再加到总和中。对于每一个连续的任期,分子的幂将被更新为+2。
I tried computing this, but it just subtracts the term when the denominator is odd, and for the next term it updates the denominator by +1 to an even number.
我试着计算,但它只是在分母为奇数时减去分母,对于下一项,它将分母+1更新为偶数。
#include <stdio.h>
#include <math.h>
int main() {
double x, sum = 0;
int n, factorial = 1;
// Input values for x and n
printf("Enter the value of x: ");
scanf("%lf", &x);
printf("Enter the value of n: ");
scanf("%d", &n);
// Calculate the sum of the series
for (int i = 0; i < n; i++) {
double term = pow(-1, i) * pow(x, 2 * i) / factorial;
sum += term;
factorial *= (i+1);
}
// Print the result
printf("The sum of the series is: %.4lf\n", sum);
return 0;
}
This code doesnt add the next term for odd denominato
此代码不会添加奇数分母的下一项
更多回答
The denominator is 1!
, 2!
, 3!
and then 3!
again. How should it continue after this?
分母是1!,2!,3!然后是3!再来一次。在这之后,它应该如何继续下去?
I assume the 4th denominator is 4!
, not 3!
.
我假设第四分母是4!,不是3!
Assuming what I mentioned above, you don't need to calculate each element from scratch: you can calculate each element using the previous one and some minimal additional computation. My answer here solves a similar problem: stackoverflow.com/questions/71098975/….
假设我上面提到的,您不需要从头开始计算每个元素:您可以使用前一个元素和一些最小的额外计算来计算每个元素。我的答案解决了一个类似的问题:Stackoverflow.com/Questions/71098975/…。
If n
is large enough then simply return exp(-x*x);
, assuming again it was supposed to be 4!
in the last term
如果n足够大,那么只需返回exp(-x*x);,再次假设它应该是4!在上个学期
优秀答案推荐
Given the comments, it seems that the wanted series looks like
从这些评论来看,这部通缉令系列看起来像是
x^0 x^2 x^4 x^6 x^8 x^10 x^12 x^14
---- - ---- + ---- - ---- + ---- - ---- + ---- - ---- + ...
1! 1! 2! 3! 3! 4! 5! 5!
In the following I'll assume that n
is the number of terms to be calculated (8 in the previous examples).
在下面,我将假设n是要计算的项数(在前面的例子中为8)。
The main issue in OP's code is that the factorial is updated at every iteration, while it shouldn't be updated one out of three times.
OP代码中的主要问题是阶乘在每次迭代时都会更新,而不应该每三次就更新一次。
Besides, calculating separately both it and the powers of x
, is more prone to numerical errors1. It would be better to just update the value of term
at each step.
此外,分开计算它和x的幂,更容易出现数值错误。最好是在每一步都更新期限的价值。
1 1 * -x^2 -x^2 * -x^2 x^4 * -x^2 -x^6 * -x^2 x^8 * -x^2 -x^10 * -x^2
--- + -------- + ----------- + ---------- + ----------- + ---------- + ------------ + ...
1 1 1 * 2 2 * 3 6 6 * 4 24 * 5
-x^2 x^4 -x^2 -x^6 x^8 -x^2
1 + 1 * -x^2 + -x^2 * ---- + --- * ---- + ---- * -x^2 + --- * ---- + ...
2 2 3 6 6 4
^^^^^ ^^^^^^ ^^^^^^ ^^^^^^ ^^^^^^
The OP could just check the value of i % 3
inside the loop body and adjust the denominator accordingly.
OP可以只检查循环体内的i%3的值,并相应地调整分母。
1) Note that in OP's code factorial
has type int
and 13! is already outside the range of a 32-bit integral type.
1)请注意,在OP的代码中,阶乘具有int和13类型!已超出32位整型的范围。
更多回答
我是一名优秀的程序员,十分优秀!