2018-09-30 23:01:58 +01:00
|
|
|
|
---
|
|
|
|
|
id: 5900f3941000cf542c50fea7
|
|
|
|
|
challengeType: 5
|
2018-10-20 21:02:47 +03:00
|
|
|
|
title: 'Problem 40: Champernowne''s constant'
|
2019-08-05 09:17:33 -07:00
|
|
|
|
forumTopicId: 302066
|
2018-09-30 23:01:58 +01:00
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## Description
|
|
|
|
|
<section id='description'>
|
2020-02-28 21:39:47 +09:00
|
|
|
|
|
2018-09-30 23:01:58 +01:00
|
|
|
|
An irrational decimal fraction is created by concatenating the positive integers:
|
2020-02-28 21:39:47 +09:00
|
|
|
|
|
2018-09-30 23:01:58 +01:00
|
|
|
|
<span style='display: block; text-align: center;'>0.12345678910<b style='color: red;'>1</b>112131415161718192021...</span>
|
2020-02-28 21:39:47 +09:00
|
|
|
|
|
2018-09-30 23:01:58 +01:00
|
|
|
|
It can be seen that the 12<sup>th</sup> digit of the fractional part is 1.
|
2020-02-28 21:39:47 +09:00
|
|
|
|
|
2018-09-30 23:01:58 +01:00
|
|
|
|
If <i>d<sub>n</sub></i> represents the <i>n</i><sup>th</sup> digit of the fractional part, find the value of the following expression.
|
2020-02-28 21:39:47 +09:00
|
|
|
|
|
2018-09-30 23:01:58 +01:00
|
|
|
|
<span style='display: block; text-align: center;'>d<sub>1</sub> × d<sub>10</sub> × d<sub>100</sub> × d<sub>1000</sub> × d<sub>10000</sub> × d<sub>100000</sub> × d<sub>1000000</sub></span>
|
2020-02-28 21:39:47 +09:00
|
|
|
|
|
2018-09-30 23:01:58 +01:00
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
## Instructions
|
|
|
|
|
<section id='instructions'>
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
## Tests
|
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
|
|
```yml
|
2018-10-04 14:37:37 +01:00
|
|
|
|
tests:
|
2020-02-28 21:39:47 +09:00
|
|
|
|
- text: <code>champernownesConstant(100)</code> should return a number.
|
|
|
|
|
testString: assert(typeof champernownesConstant(100) === 'number');
|
2018-10-04 14:37:37 +01:00
|
|
|
|
- text: <code>champernownesConstant(100)</code> should return 5.
|
2019-07-26 19:41:55 -07:00
|
|
|
|
testString: assert.strictEqual(champernownesConstant(100), 5);
|
2018-10-04 14:37:37 +01:00
|
|
|
|
- text: <code>champernownesConstant(1000)</code> should return 15.
|
2019-07-26 19:41:55 -07:00
|
|
|
|
testString: assert.strictEqual(champernownesConstant(1000), 15);
|
2018-10-04 14:37:37 +01:00
|
|
|
|
- text: <code>champernownesConstant(1000000)</code> should return 210.
|
2019-07-26 19:41:55 -07:00
|
|
|
|
testString: assert.strictEqual(champernownesConstant(1000000), 210);
|
2018-09-30 23:01:58 +01:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
function champernownesConstant(n) {
|
2020-09-15 09:57:40 -07:00
|
|
|
|
|
2018-09-30 23:01:58 +01:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
champernownesConstant(100);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
|
<section id='solution'>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
function champernownesConstant(n) {
|
2018-10-20 21:02:47 +03:00
|
|
|
|
let fractionalPart = '';
|
2018-09-30 23:01:58 +01:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</section>
|