2018-09-30 22:01:58 +00:00
|
|
|
|
---
|
|
|
|
|
id: 5900f3941000cf542c50fea7
|
2018-10-20 18:02:47 +00:00
|
|
|
|
title: 'Problem 40: Champernowne''s constant'
|
2020-11-27 18:02:05 +00:00
|
|
|
|
challengeType: 5
|
2019-08-05 16:17:33 +00:00
|
|
|
|
forumTopicId: 302066
|
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
|
|
|
|
|
2018-09-30 22:01:58 +00:00
|
|
|
|
An irrational decimal fraction is created by concatenating the positive integers:
|
2020-02-28 12:39:47 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
0.12345678910**1**112131415161718192021...
|
2020-02-28 12:39:47 +00:00
|
|
|
|
|
2018-09-30 22:01:58 +00:00
|
|
|
|
It can be seen that the 12<sup>th</sup> digit of the fractional part is 1.
|
2020-02-28 12:39:47 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
If *d<sub>n</sub>* represents the *n*<sup>th</sup> digit of the fractional part, find the value of the following expression.
|
2020-02-28 12:39:47 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
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>
|
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
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
`champernownesConstant(100)` should return a number.
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
```js
|
|
|
|
|
assert(typeof champernownesConstant(100) === 'number');
|
|
|
|
|
```
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
`champernownesConstant(100)` should return 5.
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
```js
|
|
|
|
|
assert.strictEqual(champernownesConstant(100), 5);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
`champernownesConstant(1000)` should return 15.
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
```js
|
|
|
|
|
assert.strictEqual(champernownesConstant(1000), 15);
|
2018-09-30 22:01:58 +00:00
|
|
|
|
```
|
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
`champernownesConstant(1000000)` should return 210.
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
```js
|
|
|
|
|
assert.strictEqual(champernownesConstant(1000000), 210);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
# --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
|
|
|
|
|
function champernownesConstant(n) {
|
2020-09-15 16:57:40 +00:00
|
|
|
|
|
2018-09-30 22:01:58 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
champernownesConstant(100);
|
|
|
|
|
```
|
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
# --solutions--
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
function champernownesConstant(n) {
|
2018-10-20 18:02:47 +00:00
|
|
|
|
let fractionalPart = '';
|
2018-09-30 22:01:58 +00: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;
|
|
|
|
|
}
|
|
|
|
|
```
|