2.9 KiB
id, title, challengeType, forumTopicId, dashedName
| id | title | challengeType | forumTopicId | dashedName |
|---|---|---|---|---|
| 5900f3ec1000cf542c50feff | Problem 128: Hexagonal tile differences | 1 | 301755 | 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.
By finding the difference between tile n and each of its six neighbours we shall define PD(n) to be the number of those differences which are prime.
For example, working clockwise around tile 8 the differences are 12, 29, 11, 6, 1, and 13. So PD(8) = 3.
In the same way, the differences around tile 17 are 1, 17, 16, 1, 11, and 10, hence PD(17) = 2.
It can be shown that the maximum value of PD(n) is 3.
If all of the tiles for which PD(n) = 3 are listed in ascending order to form a sequence, the 10th tile would be 271.
Find the 2000th tile in this sequence.
--hints--
hexagonalTile(10) should return 271.
assert.strictEqual(hexagonalTile(10), 271);
hexagonalTile(2000) should return 14516824220.
assert.strictEqual(hexagonalTile(2000), 14516824220);
--seed--
--seed-contents--
function hexagonalTile(tileIndex) {
return true;
}
hexagonalTile(10);
--solutions--
const NUM_PRIMES = 840000;
const PRIME_SEIVE = Array(Math.floor((NUM_PRIMES-1)/2)).fill(true);
(function initPrimes(num) {
const upper = Math.floor((num - 1) / 2);
const sqrtUpper = Math.floor((Math.sqrt(num) - 1) / 2);
for (let i = 0; i <= sqrtUpper; i++) {
if (PRIME_SEIVE[i]) {
// Mark value in PRIMES 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) {
PRIME_SEIVE[j] = false;
}
}
}
})(NUM_PRIMES);
function isPrime(num) {
if (num === 2) return true;
else if (num % 2 === 0) return false
else return PRIME_SEIVE[(num - 3) / 2];
}
function hexagonalTile(tileIndex) {
let count = 1;
let n = 1;
let number = 0;
while (count < tileIndex) {
if (isPrime(6*n - 1) &&
isPrime(6*n + 1) &&
isPrime(12*n + 5)) {
number = 3*n*n - 3*n + 2;
count++;
if (count >= tileIndex) break;
}
if (isPrime(6*n + 5) &&
isPrime(6*n - 1) &&
isPrime(12*n - 7) && n != 1) {
number = 3*n*n + 3*n + 1;
count++;
}
n++;
}
return number;
}