2018-09-30 22:01:58 +00:00
---
id: 5900f3aa1000cf542c50febd
title: 'Problem 62: Cubic permutations'
2020-11-27 18:02:05 +00:00
challengeType: 5
2019-08-05 16:17:33 +00:00
forumTopicId: 302174
2021-01-13 02:31:00 +00:00
dashedName: problem-62-cubic-permutations
2018-09-30 22:01:58 +00:00
---
2020-11-27 18:02:05 +00:00
# --description--
2020-02-28 12:39:47 +00:00
2021-06-13 11:32:54 +00:00
The cube, 41063625 ($345^3$), can be permuted to produce two other cubes: 56623104 ($384^3$) and 66430125 ($405^3$). In fact, 41063625 is the smallest cube which has exactly three permutations of its digits which are also cube.
2020-02-28 12:39:47 +00:00
2021-06-13 11:32:54 +00:00
Find the smallest cube for which exactly `n` permutations of its digits are cube.
2020-02-28 12:39:47 +00:00
2020-11-27 18:02:05 +00:00
# --hints--
2018-09-30 22:01:58 +00:00
2021-06-13 11:32:54 +00:00
`cubicPermutations(2)` should return a number.
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
```js
2021-06-13 11:32:54 +00:00
assert(typeof cubicPermutations(2) === 'number');
2020-11-27 18:02:05 +00:00
```
2018-09-30 22:01:58 +00:00
2021-06-13 11:32:54 +00:00
`cubicPermutations(2)` should return `125` .
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
```js
2021-06-13 11:32:54 +00:00
assert.strictEqual(cubicPermutations(2), 125);
```
`cubicPermutations(3)` should return `41063625` .
```js
assert.strictEqual(cubicPermutations(3), 41063625);
```
`cubicPermutations(4)` should return `1006012008` .
```js
assert.strictEqual(cubicPermutations(4), 1006012008);
```
`cubicPermutations(5)` should return `127035954683` .
```js
assert.strictEqual(cubicPermutations(5), 127035954683);
2018-09-30 22:01:58 +00:00
```
2020-11-27 18:02:05 +00:00
# --seed--
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
## --seed-contents--
2018-09-30 22:01:58 +00:00
```js
2021-06-13 11:32:54 +00:00
function cubicPermutations(n) {
2020-09-15 16:57:40 +00:00
2018-09-30 22:01:58 +00:00
return true;
}
2021-06-13 11:32:54 +00:00
cubicPermutations(2);
2018-09-30 22:01:58 +00:00
```
2020-11-27 18:02:05 +00:00
# --solutions--
2018-09-30 22:01:58 +00:00
```js
2021-06-13 11:32:54 +00:00
function cubicPermutations(n) {
function getDigits(num) {
const digits = [];
while (num > 0) {
digits.push(num % 10);
num = Math.floor(num / 10);
}
return digits;
}
function getCube(num) {
return num ** 3;
}
const digitsToCubeCounts = {};
let curNum = 1;
let digits;
while (!digitsToCubeCounts[digits] || digitsToCubeCounts[digits].count < n ) {
const cube = getCube(curNum);
digits = getDigits(cube).sort().join();
if (!digitsToCubeCounts[digits]) {
digitsToCubeCounts[digits] = {
count: 1,
smallestCube: cube
};
} else {
digitsToCubeCounts[digits].count += 1;
}
curNum++;
}
return digitsToCubeCounts[digits].smallestCube;
}
2018-09-30 22:01:58 +00:00
```