--- title: Taxicab numbers id: 594ecc0d9a8cf816e3340187 challengeType: 5 --- ## Description
A   taxicab number   (the definition that is being used here)   is a positive integer that can be expressed as the sum of two positive cubes in more than one way. The first taxicab number is   1729,   which is: 13   +   123       and 93   +   103. Taxicab numbers are also known as: *   taxi numbers *   taxi-cab numbers *   taxi cab numbers *   Hardy-Ramanujan numbers Task: Write a function that returns the lowest N taxicab numbers. For each of the taxicab numbers, show the number as well as it's constituent cubes. See also: [http://oeis.org/A001235 A001235 taxicab numbers] on The On-Line Encyclopedia of Integer Sequences. Hardy-Ramanujan Number on MathWorld. taxicab number on MathWorld. taxicab number on Wikipedia.
## Instructions
## Tests
```yml tests: - text: taxicabNumbers is a function. testString: 'assert(typeof taxicabNumbers === "function", "taxicabNumbers is a function.");' - text: taxicabNumbers should return an array. testString: 'assert(typeof taxicabNumbers(2) === "object", "taxicabNumbers should return an array.");' - text: taxicabNumbers should return an array of numbers. testString: 'assert(typeof taxicabNumbers(100)[0] === "number", "taxicabNumbers should return an array of numbers.");' - text: 'taxicabNumbers(4) must return [1729, 4104, 13832, 20683].' testString: 'assert.deepEqual(taxicabNumbers(4), res4, "taxicabNumbers(4) must return [1729, 4104, 13832, 20683].");' - text: 'taxicabNumbers(25) should return [1729, 4104, 13832, 20683, 32832, 39312, 40033, 46683, 64232, 65728, 110656, 110808, 134379, 149389, 165464, 171288, 195841, 216027, 216125, 262656, 314496, 320264, 327763, 373464, 402597]' testString: 'assert.deepEqual(taxicabNumbers(25), res25, "taxicabNumbers(25) should return [1729, 4104, 13832, 20683, 32832, 39312, 40033, 46683, 64232, 65728, 110656, 110808, 134379, 149389, 165464, 171288, 195841, 216027, 216125, 262656, 314496, 320264, 327763, 373464, 402597]");' - text: 'taxicabNumbers(39) resulting numbers from 20 - 29 should be [314496,320264,327763,373464,402597,439101,443889,513000,513856].' testString: 'assert.deepEqual(taxicabNumbers(39).slice(20, 29), res39From20To29, "taxicabNumbers(39) resulting numbers from 20 - 29 should be [314496,320264,327763,373464,402597,439101,443889,513000,513856].");' ```
## Challenge Seed
```js function taxicabNumbers (n) { // Good luck! return true; } ```
### After Test
```js console.info('after the test'); ```
## Solution
```js function taxicabNumbers(nNumbers) { const cubeN = []; const s3s = {}; const e = 100; for (let n = 1; n < e; n += 1) { cubeN[n] = n * n * n; } for (let a = 1; a < e - 1; a += 1) { const a3 = cubeN[a]; for (let b = a; b < e; b += 1) { const b3 = cubeN[b]; const s3 = a3 + b3; let abs = s3s[s3]; if (!abs) { s3s[s3] = abs = []; } abs.push([a, b]); } } let i = 0; const res = []; Object.keys(s3s).forEach(s3 => { const abs = s3s[s3]; if (abs.length >= 2) { // No two cube pairs found i += 1; if (i <= nNumbers) { res.push(s3); } } }); return res.map(item => parseInt(item, 10)); } ```