我正在尝试为 n = 60 以内的值编写此递归序列。我知道 x0 = 1。
Xn+1 = 2^(n+1) * (sqrt( 1 + 2^-n*Xn) - 1)
到目前为止我已经
int main ()
{
// first term x0 = 1
double xn = 1;
double x;
double squareRoot = 1 + (pow(2, -x) * xn);
for (x = 1; x <= 60; x++)
{
double xn = pow(2, x) * double sqrt(squareRoot) - 1);
printf("%f\n", xn);
}
}
但是我在有双 sqrt 的行上得到了预期的表达式错误。
最佳答案
1.这里有一个额外的括号。
double xn = pow(2, x) * double sqrt(squareRoot) - 1);
并且您不需要在此处提及类型 -double sqrt(squareRoot)
。你可以这样写 -
double xn = pow(2, x) * (sqrt(squareRoot) - 1);
2.此声明也会产生问题 -
double x;
double squareRoot = 1 + (pow(2, -x) * xn); //this statement
for (x = 1; x <= 60; x++)
这里您在pow
中使用了x
,但它在循环之外,并且x
直到循环才初始化。因此,您需要在此语句之前初始化x
。
关于C 计算递归序列 - 预期表达式错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32857364/