2018-09-30 23:01:58 +01:00
---
title: 9 billion names of God the integer
id: 5949b579404977fbaefcd736
challengeType: 5
2019-08-05 09:17:33 -07:00
forumTopicId: 302219
2018-09-30 23:01:58 +01:00
---
## Description
<section id='description'>
2019-03-01 17:10:50 +09:00
This task is a variation of the <a href='https://en.wikipedia.org/wiki/The Nine Billion Names of God#Plot_summary ' title='wp: The Nine Billion Names of God#Plot_summary ' target='_blank'>short story by Arthur C. Clarke</a>.
2019-02-24 19:04:29 +09:00
(Solvers should be aware of the consequences of completing this task.)
2019-05-22 23:41:41 +09:00
In detail, to specify what is meant by a "name":
2019-03-01 17:10:50 +09:00
<ul>
2019-05-22 23:41:41 +09:00
<li>The integer 1 has 1 name "1".</li>
<li>The integer 2 has 2 names "1+1" and "2".</li>
<li>The integer 3 has 3 names "1+1+1", "2+1", and "3".</li>
<li>The integer 4 has 5 names "1+1+1+1", "2+1+1", "2+2", "3+1", "4".</li>
<li>The integer 5 has 7 names "1+1+1+1+1", "2+1+1+1", "2+2+1", "3+1+1", "3+2", "4+1", "5".</li>
2019-03-01 17:10:50 +09:00
</ul>
2019-02-24 19:04:29 +09:00
This can be visualized in the following form:
2018-09-30 23:01:58 +01:00
<pre>
1
1 1
1 1 1
1 2 1 1
1 2 2 1 1
1 3 3 2 1 1
</pre>
2019-02-24 19:04:29 +09:00
Where row $n$ corresponds to integer $n$, and each column $C$ in row $m$ from left to right corresponds to the number of names beginning with $C$.
2019-05-23 13:57:59 +09:00
Optionally note that the sum of the $n$-th row $P(n)$ is the integer partition function.
2018-09-30 23:01:58 +01:00
</section>
## Instructions
<section id='instructions'>
2019-03-01 17:10:50 +09:00
Implement a function that returns the sum of the $n$-th row.
2018-09-30 23:01:58 +01:00
</section>
## Tests
<section id='tests'>
```yml
2018-10-04 14:37:37 +01:00
tests:
- text: <code>numberOfNames</code> is a function.
2019-07-26 05:24:52 -07:00
testString: assert(typeof numberOfNames === 'function');
2018-10-04 14:37:37 +01:00
- text: <code>numberOfNames(5)</code> should equal 7.
2019-07-26 05:24:52 -07:00
testString: assert.equal(numberOfNames(5), 7);
2018-10-04 14:37:37 +01:00
- text: <code>numberOfNames(12)</code> should equal 77.
2019-07-26 05:24:52 -07:00
testString: assert.equal(numberOfNames(12), 77);
2018-10-04 14:37:37 +01:00
- text: <code>numberOfNames(18)</code> should equal 385.
2019-07-26 05:24:52 -07:00
testString: assert.equal(numberOfNames(18), 385);
2018-10-04 14:37:37 +01:00
- text: <code>numberOfNames(23)</code> should equal 1255.
2019-07-26 05:24:52 -07:00
testString: assert.equal(numberOfNames(23), 1255);
2018-10-04 14:37:37 +01:00
- text: <code>numberOfNames(42)</code> should equal 53174.
2019-07-26 05:24:52 -07:00
testString: assert.equal(numberOfNames(42), 53174);
2018-10-04 14:37:37 +01:00
- text: <code>numberOfNames(123)</code> should equal 2552338241.
2019-07-26 05:24:52 -07:00
testString: assert.equal(numberOfNames(123), 2552338241);
2018-09-30 23:01:58 +01:00
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
```js
2019-02-24 19:04:29 +09:00
function numberOfNames(num) {
2018-09-30 23:01:58 +01:00
// Good luck!
return true;
}
```
</div>
</section>
## Solution
<section id='solution'>
```js
2019-02-26 17:07:07 +09:00
function numberOfNames(num) {
2018-09-30 23:01:58 +01:00
const cache = [
[1]
];
for (let l = cache.length; l < num + 1; l++) {
let Aa;
let Mi;
const r = [0];
for (let x = 1; x < l + 1; x++) {
r.push(r[r.length - 1] + (Aa = cache[l - x < 0 ? cache.length - (l - x) : l - x])[(Mi = Math.min(x, l - x)) < 0 ? Aa.length - Mi : Mi]);
}
cache.push(r);
}
return cache[num][cache[num].length - 1];
}
```
</section>