1

Is there a formula for cumulative or summation of terms in a quadratic equation? I need an O(1) formula since I need to put this into a code. Thanks.

Here's snapshot of my spreadsheet. Currently, I'm just bruteforcing the cumulative value.

enter image description here

Sylpheed
  • 135

1 Answers1

2

Indeed, I have the perfect formula for you! $$\sum_{r=1}^n 1000r^2+3000=\frac{500}{3}n(n+1)(2n+1)+3000n$$ Ok, now for the derivation. I'll give a brief explanation of sigma ($\sum$)notation. If, for example, we have the series $1+2+3+4...$ tat is the sum of the sequence r and is represented by $\sum_{r=1}^n r$. There are some results of series which are useful: $$\sum_{r=1}^n (f(r)+g(r))=\sum_{r=1}^n f(r)+\sum_{r=1}^n g(r)$$ where $f(r)$ and $g(r)$ are functions of $r$, eg $f(r)=r, g(r)=5r^2$. Another very useful result is the following, where $k$ is a constant: $$\sum_{r=1}^n kf(r)=k\sum_{r=1}^n f(r)$$ We can apply these rules together with more complicated series. The following results are standard: $$\sum_{r=1}^n 1 = n$$ $$\sum_{r=1}^n r^2 = \frac{n(n+1)(2n+1)}{6}$$ These specific results are useful in your query. You are actually asking: What is the general formula for $\sum_{r=1}^n 1000r^2+3000$? Let's use the rules we just established: $$\sum_{r=1}^n 1000r^2+3000=\sum_{r=1}^n 1000r^2+\sum_{r=1}^n 3000=1000\sum_{r=1}^n r^2+3000\sum_{r=1}^n 1=\frac{1000n(n+1)(2n+1)}{6}+3000n=\frac{500}{3}n(n+1)(2n+1)+3000n$$ when simplified. I hope that helped!

  • Thanks. Yep, it would help if I can see how to derive this. The key here is that a,b,c are variables (which can change anytime). – Sylpheed Jun 10 '20 at 04:10
  • Do you understand how to use the formula though? Did I help you? I'll show its derivation soon. – A-Level Student Jun 10 '20 at 08:56
  • Thanks! Finally understood it lol. I'm not used to reading mathematical expression since I'm mostly writing code. Just realized that you computed the constant c (or k in your case) separately from ax^2. I didn't think that you can just simply derive each term separately. – Sylpheed Jun 11 '20 at 10:39
  • I translated your formula to code and came up with this (a bit messy to read). sequence = (a * n * (n+1) * (2n + 1) / 6) + (c * n). It now matches the values in my spreadsheet. Big thanks! – Sylpheed Jun 11 '20 at 10:56
  • You are very welcome :)) – A-Level Student Jun 11 '20 at 13:10