2018-12-30 13:35:56 +00:00
---
id: 5a23c84252665b21eecc8041
title: Sum of a series
challengeType: 5
2019-08-05 16:17:33 +00:00
forumTopicId: 302333
2021-01-13 02:31:00 +00:00
dashedName: sum-of-a-series
2018-12-30 13:35:56 +00:00
---
2020-11-27 18:02:05 +00:00
# --description--
2020-03-30 16:23:18 +00:00
2020-11-27 18:02:05 +00:00
Compute the **n** < sup > th</ sup > term of a [series ](<https://en.wikipedia.org/wiki/Series (mathematics )>), i.e. the sum of the **n** first terms of the corresponding [sequence ](https://en.wikipedia.org/wiki/sequence ). Informally this value, or its limit when **n** tends to infinity, is also called the *sum of the series* , thus the title of this task. For this task, use: $S*n = \\sum*{k=1}^n \\frac{1}{k^2}$ and compute $S\_{1000}$ This approximates the [zeta function ](<https://en.wikipedia.org/wiki/Riemann zeta function> ) for S=2, whose exact value $\\zeta(2) = {\\pi^2\\over 6}$ is the solution of the [Basel problem ](<https://en.wikipedia.org/wiki/Basel problem> ).
2019-07-18 15:32:12 +00:00
2020-11-27 18:02:05 +00:00
# --instructions--
2020-03-30 16:23:18 +00:00
2020-11-27 18:02:05 +00: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.
2018-12-30 13:35:56 +00:00
2020-11-27 18:02:05 +00:00
# --hints--
2020-03-30 16:23:18 +00:00
2020-11-27 18:02:05 +00:00
`sum` should be a function.
2019-07-18 15:32:12 +00:00
2020-11-27 18:02:05 +00:00
```js
assert(typeof sum == 'function');
```
2020-03-30 16:23:18 +00:00
2020-11-27 18:02:05 +00:00
`sum(1, 100)` should return a number.
```js
assert(typeof sum(1, 100) == 'number');
2018-12-30 13:35:56 +00:00
```
2020-11-27 18:02:05 +00:00
`sum(1, 100)` should return `1.6349839001848923` .
```js
assert.equal(sum(1, 100), 1.6349839001848923);
```
2018-12-30 13:35:56 +00:00
2020-11-27 18:02:05 +00:00
`sum(33, 46)` should return `0.009262256361481223` .
2020-03-30 16:23:18 +00:00
2020-11-27 18:02:05 +00:00
```js
assert.equal(sum(33, 46), 0.009262256361481223);
```
2019-07-18 15:32:12 +00:00
2020-11-27 18:02:05 +00:00
`sum(21, 213)` should return `0.044086990748706555` .
2018-12-30 13:35:56 +00:00
```js
2020-11-27 18:02:05 +00:00
assert.equal(sum(21, 213), 0.044086990748706555);
```
2020-09-15 16:57:40 +00:00
2020-11-27 18:02:05 +00:00
`sum(11, 111)` should return `0.08619778593108679` .
```js
assert.equal(sum(11, 111), 0.08619778593108679);
2018-12-30 13:35:56 +00:00
```
2020-11-27 18:02:05 +00:00
`sum(1, 10)` should return `1.5497677311665408` .
2018-12-30 13:35:56 +00:00
2020-11-27 18:02:05 +00:00
```js
assert.equal(sum(1, 10), 1.5497677311665408);
```
2020-03-30 16:23:18 +00:00
2020-11-27 18:02:05 +00:00
# --seed--
## --seed-contents--
```js
function sum(a, b) {
}
```
# --solutions--
2018-12-30 13:35:56 +00:00
```js
2019-03-10 10:14:48 +00:00
function sum(a, b) {
2018-12-30 13:35:56 +00:00
function fn(x) {
2020-03-30 16:23:18 +00:00
return 1 / (x * x);
2018-12-30 13:35:56 +00:00
}
var s = 0;
for (; a < = b; a++) s += fn(a);
return s;
}
```