2018-12-30 19:05:56 +05:30
---
id: 5a23c84252665b21eecc8041
title: Sum of a series
challengeType: 5
2019-08-05 09:17:33 -07:00
forumTopicId: 302333
2018-12-30 19:05:56 +05:30
---
## Description
2020-03-30 11:23:18 -05:00
2018-12-30 19:05:56 +05:30
<section id='description'>
2019-07-18 17:32:12 +02:00
2020-03-30 11:23:18 -05:00
Compute the <b>n</b><sup>th</sup> term of a <a href="https://en.wikipedia.org/wiki/Series (mathematics)" target="_blank">series</a>, i.e. the sum of the <b>n</b> first terms of the corresponding <a href="https://en.wikipedia.org/wiki/sequence" target="_blank">sequence</a>.
Informally this value, or its limit when <b>n</b> tends to infinity, is also called the <i>sum of the series</i>, thus the title of this task.
2018-12-30 19:05:56 +05:30
For this task, use:
2019-03-10 19:14:48 +09:00
<span style="margin-left: 2em;">$S_n = \sum_{k=1}^n \frac{1}{k^2}$</span>
2020-03-30 11:23:18 -05:00
and compute $S_{1000}$
This approximates the <a href="https://en.wikipedia.org/wiki/Riemann zeta function" target="_blank">zeta function</a> for S=2, whose exact value
2019-03-10 19:14:48 +09:00
<span style="margin-left: 2em;">$\zeta(2) = {\pi^2\over 6}$</span>
2020-03-30 11:23:18 -05:00
is the solution of the <a href="https://en.wikipedia.org/wiki/Basel problem" target="_blank">Basel problem</a>.
2018-12-30 19:05:56 +05:30
</section>
## Instructions
2020-03-30 11:23:18 -05:00
2018-12-30 19:05:56 +05:30
<section id='instructions'>
2019-07-18 17:32:12 +02:00
2019-03-10 19:14:48 +09:00
Write a function that take $a$ and $b$ as parameters and returns the sum of $a^{th}$ to $b^{th}$ members of the sequence.
2020-03-30 11:23:18 -05:00
2018-12-30 19:05:56 +05:30
</section>
## Tests
2020-03-30 11:23:18 -05:00
2018-12-30 19:05:56 +05:30
<section id='tests'>
2020-03-30 11:23:18 -05:00
```yml
2018-12-30 19:05:56 +05:30
tests:
- text: <code>sum</code> should be a function.
2020-03-30 11:23:18 -05:00
testString: assert(typeof sum == 'function');
2018-12-30 19:05:56 +05:30
- text: <code>sum(1, 100)</code> should return a number.
2020-03-30 11:23:18 -05:00
testString: assert(typeof sum(1, 100) == 'number');
2018-12-30 19:05:56 +05:30
- text: <code>sum(1, 100)</code> should return <code>1.6349839001848923</code>.
2020-03-30 11:23:18 -05:00
testString: assert.equal(sum(1, 100), 1.6349839001848923);
2018-12-30 19:05:56 +05:30
- text: <code>sum(33, 46)</code> should return <code>0.009262256361481223</code>.
2020-03-30 11:23:18 -05:00
testString: assert.equal(sum(33, 46), 0.009262256361481223);
2018-12-30 19:05:56 +05:30
- text: <code>sum(21, 213)</code> should return <code>0.044086990748706555</code>.
2020-03-30 11:23:18 -05:00
testString: assert.equal(sum(21, 213), 0.044086990748706555);
2018-12-30 19:05:56 +05:30
- text: <code>sum(11, 111)</code> should return <code>0.08619778593108679</code>.
2020-03-30 11:23:18 -05:00
testString: assert.equal(sum(11, 111), 0.08619778593108679);
2018-12-30 19:05:56 +05:30
- text: <code>sum(1, 10)</code> should return <code>1.5497677311665408</code>.
2020-03-30 11:23:18 -05:00
testString: assert.equal(sum(1, 10), 1.5497677311665408);
2018-12-30 19:05:56 +05:30
```
</section>
## Challenge Seed
2020-03-30 11:23:18 -05:00
2018-12-30 19:05:56 +05:30
<section id='challengeSeed'>
2019-07-18 17:32:12 +02:00
2018-12-30 19:05:56 +05:30
<div id='js-seed'>
```js
2019-03-10 19:14:48 +09:00
function sum(a, b) {
2018-12-30 19:05:56 +05:30
// Good luck!
}
```
</div>
</section>
## Solution
2020-03-30 11:23:18 -05:00
2018-12-30 19:05:56 +05:30
<section id='solution'>
```js
2019-03-10 19:14:48 +09:00
function sum(a, b) {
2018-12-30 19:05:56 +05:30
function fn(x) {
2020-03-30 11:23:18 -05:00
return 1 / (x * x);
2018-12-30 19:05:56 +05:30
}
var s = 0;
for (; a <= b; a++) s += fn(a);
return s;
}
```
2019-07-18 17:32:12 +02:00
</section>