序列的总和

发布于 2025-01-26 23:27:54 字数 815 浏览 5 评论 0原文

我正在尝试制定一个功能来计算此公式

#include <iostream>
#include <vector>
double Sequence(std::vector < double > & a) {
  double result = 0;
  for (int i = a.size() - 1; i > 0; i--) {
    if (a[i] == 0) throw std::domain_error("Dividing with 0");
    if (i > 1)
      result += 1 / (a[i - 1] + 1 / a[i]);
    else result += a[i - i];
    std::cout << a[i] << " " << result << " " << "\n";
  }
  return result;
}
int main() {
  std::vector<double>a{1,2,3,4,5};
  try {
    std::cout << Sequence(a);
  } catch (std::domain_error e) {
    std::cout << e.what();
  }

  return 0;
}

​但是,如果我在该序列结果中添加了几个数字,则会出错。您能帮我解决这个问题吗?

I'm trying to make a function for calculating this formula

formula

#include <iostream>
#include <vector>
double Sequence(std::vector < double > & a) {
  double result = 0;
  for (int i = a.size() - 1; i > 0; i--) {
    if (a[i] == 0) throw std::domain_error("Dividing with 0");
    if (i > 1)
      result += 1 / (a[i - 1] + 1 / a[i]);
    else result += a[i - i];
    std::cout << a[i] << " " << result << " " << "\n";
  }
  return result;
}
int main() {
  std::vector<double>a{1,2,3,4,5};
  try {
    std::cout << Sequence(a);
  } catch (std::domain_error e) {
    std::cout << e.what();
  }

  return 0;
}

Code gives correct result for {1,2,3} sequence. However, if I add a few numbers to that sequence result becomes wrong. Could you help me to fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

从此见与不见 2025-02-02 23:27:54
if (i > 1)
  result += 1 / (a[i - 1] + 1 / a[i]);
else result += a[i - i];

这是错误的;如果您有三个或更少的条款,它恰好可以工作。

您实际想要的复发是

if (i == a.size() - 1) {
    result = a[i];
} else {
    result = a[i] + 1 / result;
}

您可以看到,这是正确的复发。
整理更多的东西(从环内部删除条件,在循环终止条件下逐个固定,并纠正异常条件):

double Sequence(std::vector<double> &a) {
  double result = a[a.size() - 1];
  for (int i = a.size() - 1; i >= 0; i--) {
    if (result == 0)
      throw std::domain_error("Dividing with 0");
    result = a[i] + 1 / result;
  }
  return result;
}
if (i > 1)
  result += 1 / (a[i - 1] + 1 / a[i]);
else result += a[i - i];

This is wrong; it just happens to work if you have three or fewer terms.

The recurrence you actually want is

if (i == a.size() - 1) {
    result = a[i];
} else {
    result = a[i] + 1 / result;
}

you can see that this is a correct recurrence by the fact that
Tidying up some more things (removing the condition from inside the loop, fixing an off-by-one in the loop termination condition, and correcting the exception condition):

double Sequence(std::vector<double> &a) {
  double result = a[a.size() - 1];
  for (int i = a.size() - 1; i >= 0; i--) {
    if (result == 0)
      throw std::domain_error("Dividing with 0");
    result = a[i] + 1 / result;
  }
  return result;
}
彩扇题诗 2025-02-02 23:27:54

这被称为简单的持续分数,所有分子都是1s。它基本上表示为[a1; a2,a3,...,a](实际上是从a0开始的,但无论如何)。您可以在每个步骤中从上到下计算结果,同时获得更好的近似值(或 convergent )。

这个有限的理性系列或无限非理性系列的结果表示为p/q。为了计算您假设的结果

p0   1     p1   a1       p2   a2*p1+p0      p3   a3*p2+p1            pn   a_n*p_n-1+p_n-2
__ = _ and __ = __ then  __ = ________ then __ = ________ .. then .. __ = _______________
q0   0     q1   1        q2   a2*q1+q0      q3   a3*q2+q1            qn   a_n*q_n-1+q_n-2

,并且每个下一个p_x/q_x为结果提供了更好的appoximation。

说如果给您一个小数,例如1.425在持续的分数胶囊中,则如

[1; 2, 2, 1, 5]

A.KA

          1
1 + _____________
            1
    2 + _________
              1
        2 + _____
                1
            1 + _
                5

,那么如上所述计算的中间收敛;

1    1    3    7    10    57
_ -> _ -> _ -> _ -> __ -> __ = 1.425
0    1    2    5    7     40

请注意,每个收敛性的过冲都从最终结果中上下移动,随后除了57/401.425的最小合理表达式。

持续的分数是一个非常深的话题,实际上,一旦以持续的分数形式建立了算术,就消除了浮点误差。

您可以玩在这里

This known as the Simple Continued Fraction where all numerators are 1s. It is basically represented as [a1;a2,a3,...,an] (well in fact starts from a0 but whatever). You can calculate the result from top to bottom while obtaining a better approximation (or convergent as they call it) at each step.

The result of this finite rational series or infinite irrational series is expressed as p/q. In order to calculate the result you assume

p0   1     p1   a1       p2   a2*p1+p0      p3   a3*p2+p1            pn   a_n*p_n-1+p_n-2
__ = _ and __ = __ then  __ = ________ then __ = ________ .. then .. __ = _______________
q0   0     q1   1        q2   a2*q1+q0      q3   a3*q2+q1            qn   a_n*q_n-1+q_n-2

and every next p_x/q_x yields a better appoximation to the result.

Say if you are given a decimal like 1.425 in continued fraction cefficients as

[1; 2, 2, 1, 5]

a.k.a.

          1
1 + _____________
            1
    2 + _________
              1
        2 + _____
                1
            1 + _
                5

Then the intermediate convergents calculated as described above would be;

1    1    3    7    10    57
_ -> _ -> _ -> _ -> __ -> __ = 1.425
0    1    2    5    7     40

Notice that each convergent overshoots up and down from the final result subsequently besides 57/40 being the minimal rational expression of 1.425.

Continued fractions are a very deep topic which in fact eliminates the floating point error once an arithmetic is establised among them in their continued fractions form.

You can play here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文