Files

1.8 KiB
Raw Permalink Blame History

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
5900f3941000cf542c50fea7 Завдання 40: Константа Чамперноуна 5 302066 problem-40-champernownes-constant

--description--

Ірраціональний десятковий дріб створюється шляхом об’єднання натуральних чисел:

0.123456789101112131415161718192021...

Можна побачити, що 12та цифра дробової частини - 1.

Якщо d являє собою n-ту цифру дробової частини, знайдіть значення наступного виразу.

d1 × d10 × d100 × d1000 × d10000 × d100000 × d1000000

--hints--

champernownesConstant(100) має повернути число.

assert(typeof champernownesConstant(100) === 'number');

champernownesConstant(100) має повернути число 5.

assert.strictEqual(champernownesConstant(100), 5);

champernownesConstant(1000) має повернути число 15.

assert.strictEqual(champernownesConstant(1000), 15);

champernownesConstant(1000000) має повернути число 210.

assert.strictEqual(champernownesConstant(1000000), 210);

--seed--

--seed-contents--

function champernownesConstant(n) {

  return true;
}

champernownesConstant(100);

--solutions--

function champernownesConstant(n) {
  let fractionalPart = '';
  for (let i = 0; fractionalPart.length <= n; i++) {
    fractionalPart += i.toString();
  }

  let product = 1;
  for (let i = 0; i < n.toString().length; i++) {
    const index = 10 ** i;
    product *= parseInt(fractionalPart[index], 10);
  }

  return product;
}