Skip to content

Instantly share code, notes, and snippets.

@bgoonz
Created March 31, 2024 16:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bgoonz/b39f10a024c881cc0e0b16545a6e8072 to your computer and use it in GitHub Desktop.
Save bgoonz/b39f10a024c881cc0e0b16545a6e8072 to your computer and use it in GitHub Desktop.
PrimeTripplets.js
function isPrime(num) {
sRoot = Math.sqrt(num);
for (let i = 2; i <= sRoot; i++) if (num % i === 0) return false;
return num > 1;
}
function findPrimeTriplets() {
const twoDigitPrimes = [];
for (let i = 11; i <= 99; i++) {
if (isPrime(i)) {
twoDigitPrimes.push(i);
}
}
for (let i = 0; i < twoDigitPrimes.length; i++) {
for (let j = i + 1; j < twoDigitPrimes.length; j++) {
for (let k = j + 1; k < twoDigitPrimes.length; k++) {
const a = twoDigitPrimes[i];
const b = twoDigitPrimes[j];
const c = twoDigitPrimes[k];
const avgAB = (a + b) / 2;
const avgAC = (a + c) / 2;
const avgBC = (b + c) / 2;
const avgAll = (a + b + c) / 3;
if (isPrime(avgAB) && isPrime(avgAC) && isPrime(avgBC) && isPrime(avgAll)) {
return [a, b, c];
}
}
}
}
return null;
}
console.log(findPrimeTriplets());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment