Files
freeCodeCamp/curriculum/challenges/chinese/10-coding-interview-prep/project-euler/problem-128-hexagonal-tile-differences.md
2022-10-24 10:55:50 -07:00

118 lines
3.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
id: 5900f3ec1000cf542c50feff
title: 'Problem 128: Hexagonal tile differences'
challengeType: 1
forumTopicId: 301755
dashedName: problem-128-hexagonal-tile-differences
---
# --description--
A hexagonal tile with number 1 is surrounded by a ring of six hexagonal tiles, starting at "12 o'clock" and numbering the tiles 2 to 7 in an anti-clockwise direction.
New rings are added in the same fashion, with the next rings being numbered 8 to 19, 20 to 37, 38 to 61, and so on. The diagram below shows the first three rings.
<img class="img-responsive center-block" alt="前三圈排列好的六角砖,数字编号为 1 到 37其中砖 8 和砖 17高亮" src="https://cdn.freecodecamp.org/curriculum/project-euler/hexagonal-tile-differences.png" style="background-color: white; padding: 10px;" />
通过计算砖 $n$ 和它周围 6 块砖的数字差,我们定位 $PD(n)$ 为数字差中素数的个数。
例如,围绕砖 8 顺时针方向的差额分别为 12、29、11、6、1 和 13。 则 $PD(8) = 3$。
同理,围绕砖 17 的差额为 1、17、16、1、11 和 10所以 $PD(17) = 2$。
可以发现 $PD(n)$ 的最大值是 $3$。
如果 $PD(n) = 3$ 的砖按升序排列,那么第 10 块砖将会是 271。
求序列中的第 2000 块砖。
# --hints--
`hexagonalTile(10)` should return `271`.
```js
assert.strictEqual(hexagonalTile(10), 271);
```
`hexagonalTile(2000)` should return `14516824220`.
```js
assert.strictEqual(hexagonalTile(2000), 14516824220);
```
# --seed--
## --seed-contents--
```js
function hexagonalTile(tileIndex) {
return true;
}
hexagonalTile(10);
```
# --solutions--
```js
class PrimeSeive {
constructor(num) {
const seive = Array(Math.floor((num - 1) / 2)).fill(true);
const upper = Math.floor((num - 1) / 2);
const sqrtUpper = Math.floor((Math.sqrt(num) - 1) / 2);
for (let i = 0; i <= sqrtUpper; i++) {
if (seive[i]) {
// Mark value in seive array
const prime = 2 * i + 3;
// Mark all multiples of this number as false (not prime)
const primeSqaredIndex = 2 * i ** 2 + 6 * i + 3;
for (let j = primeSqaredIndex; j < upper; j += prime) {
seive[j] = false;
}
}
}
this._seive = seive;
}
isPrime(num) {
return num === 2
? true
: num % 2 === 0
? false
: this.isOddPrime(num);
}
isOddPrime(num) {
return this._seive[(num - 3) / 2];
}
};
function hexagonalTile(tileIndex) {
const primeSeive = new PrimeSeive(tileIndex * 420);
let count = 1;
let n = 1;
let number = 0;
while (count < tileIndex) {
if (primeSeive.isPrime(6*n - 1) &&
primeSeive.isPrime(6*n + 1) &&
primeSeive.isPrime(12*n + 5)) {
number = 3*n*n - 3*n + 2;
count++;
if (count >= tileIndex) break;
}
if (primeSeive.isPrime(6*n + 5) &&
primeSeive.isPrime(6*n - 1) &&
primeSeive.isPrime(12*n - 7) && n != 1) {
number = 3*n*n + 3*n + 1;
count++;
}
n++;
}
return number;
}
```