freeCodeCamp/curriculum/challenges/portuguese/10-coding-interview-prep/project-euler/problem-62-cubic-permutatio...

2.0 KiB

id title challengeType forumTopicId dashedName
5900f3aa1000cf542c50febd Problema 62: Permutações cúbicas 5 302174 problem-62-cubic-permutations

--description--

O número 41063625 é o resultado do cubo (345^3). Esse número pode ser permutado para produzir dois outros números que também são resultados de um cubo: 56623104 (384^3) e 66430125 (405^3). 41063625 é o menor número que tem exatamente três permutações dos seus dígitos e essas permutações também são o resultado de um cubo.

Encontre o menor cubo onde n permutações de seus dígitos são cubos.

--hints--

cubicPermutations(2) deve retornar um número.

assert(typeof cubicPermutations(2) === 'number');

cubicPermutations(2) deve retornar 125.

assert.strictEqual(cubicPermutations(2), 125);

cubicPermutations(3) deve retornar 41063625.

assert.strictEqual(cubicPermutations(3), 41063625);

cubicPermutations(4) deve retornar 1006012008.

assert.strictEqual(cubicPermutations(4), 1006012008);

cubicPermutations(5) deve retornar 127035954683.

assert.strictEqual(cubicPermutations(5), 127035954683);

--seed--

--seed-contents--

function cubicPermutations(n) {

  return true;
}

cubicPermutations(2);

--solutions--

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;
}