(euler) make puzzles functional

This commit is contained in:
newt 2024-10-09 18:02:35 +01:00
parent 6b17075cfa
commit 563866d555
7 changed files with 73 additions and 44 deletions

View file

@ -8,6 +8,7 @@
"devDependencies": {
"@types/node": "^16.11.6",
"chalk": "^4.1.2",
"execution-time": "^1.4.1",
"gulp": "^4.0.2",
"gulp-cli": "^2.3.0",
"gulp-rename": "^2.0.0",

View file

@ -1,6 +1,7 @@
const fs = require('fs');
const chalk = require('chalk');
const { spawnSync } = require('child_process');
const perf = require('execution-time')();
const run = (file, puzzleName) => {
// Calculate the length of the divider for the puzzle
@ -10,12 +11,19 @@ const run = (file, puzzleName) => {
divider += '-';
}
// Log output
// Styling
console.log(divider);
console.log(chalk.bold(chalk.greenBright(puzzleName)));
console.log(divider);
// Execute the file
perf.start();
spawnSync('node', [`build/${file}`], { shell: true, stdio: 'inherit' });
const results = perf.stop();
// Print time results
console.log();
console.log(chalk.bold(chalk.yellow(`Executed in ${results.words}`)));
};
// Get files

View file

@ -1,12 +1,13 @@
import { calcSum } from './utils';
const below = 1000;
const multiplesOf = [3, 5];
const toAdd: Set<number> = new Set();
const multiplesOf = (numbers: number[], upperBound: number) => {
const results: Set<number> = new Set();
// Find all of the multiples of 3 and 5 below 1000
for (let i = 1; i < below; i++) {
multiplesOf.forEach(num => (i % num == 0 ? toAdd.add(i) : null));
}
for (let i = 1; i < upperBound; i++) {
numbers.forEach(num => (i % num == 0 ? results.add(i) : null));
}
console.log(calcSum(Array.from(toAdd)));
return Array.from(results);
};
console.log(calcSum(multiplesOf([3, 5], 1000)));

View file

@ -1,15 +1,22 @@
import { calcSum } from './utils';
const sequence = [1, 2];
const max = 4000000;
const fibonacciNumbers = (upperBound: number) => {
const sequence = [1, 2];
// Keep making new nujm
while (sequence[sequence.length - 1] < max) {
const newValue = sequence[sequence.length - 1] + sequence[sequence.length - 2];
// Keep making new numbers in the sequence until we hit the upper bound
while (sequence[sequence.length - 1] < upperBound) {
const newValue = sequence[sequence.length - 1] + sequence[sequence.length - 2];
sequence.push(newValue);
}
sequence.push(newValue);
}
return sequence;
};
// Filter out the even numbers and find the sum
const even = sequence.filter(n => n % 2 === 0);
console.log(calcSum(even));
const evenFibonacciNumbers = (upperBound: number) => {
const sequence = fibonacciNumbers(upperBound);
const even = sequence.filter(n => n % 2 === 0);
return even;
};
console.log(calcSum(evenFibonacciNumbers(4000000)));

View file

@ -1,9 +1,12 @@
let number = 600851475143;
let i = 2;
const largestPrimeFactor = (number: number) => {
let i = 2;
while (i * i <= number) {
if (number % i) i += 1;
else number = Math.floor(number / i);
}
while (i * i <= number) {
if (number % i) i += 1;
else number = Math.floor(number / i);
}
console.log(number);
return number;
};
console.log(largestPrimeFactor(600851475143));

View file

@ -1,22 +1,21 @@
const biggest = 999;
const smallest = 100;
const largestPallidromeNumber = (lowerBound: number, upperBound: number) => {
// Work out all of the products of 3 digit numbers
const products: number[] = [];
// Work out all of the products of 3 digit numbers
const products: number[] = [];
for (let i = smallest; i < biggest + 1; i++) {
for (let j = smallest; j < biggest + 1; j++) {
products.push(i * j);
for (let i = lowerBound; i < upperBound + 1; i++) {
for (let j = lowerBound; j < upperBound + 1; j++) {
products.push(i * j);
}
}
}
// Filter for palindromic numbers
const palindromic = products.filter(
x => x.toString() === x.toString().split('').reverse().join('')
);
// Filter for palindromic numbers
const palindromic = products.filter(
x => x.toString() === x.toString().split('').reverse().join('')
);
// Find the biggest palindrome number
const sorted = palindromic.sort((a, b) => b - a);
const largest = sorted[0];
// Find the biggest palindrome number
const sorted = palindromic.sort((a, b) => b - a);
return sorted[0];
};
console.log(largest);
console.log(largestPallidromeNumber(100, 999));

View file

@ -926,6 +926,15 @@ __metadata:
languageName: node
linkType: hard
"execution-time@npm:^1.4.1":
version: 1.4.1
resolution: "execution-time@npm:1.4.1"
dependencies:
pretty-hrtime: ^1.0.3
checksum: 0a5ef5604fc1ed9f626126443dfb52cd94926a657384433a3737e7878e1697e9b37e04e63a22e2ae2f6ac6bd20261f58fb8c890388f6d3fdf906425fca98be3d
languageName: node
linkType: hard
"expand-brackets@npm:^2.1.4":
version: 2.1.4
resolution: "expand-brackets@npm:2.1.4"
@ -2627,7 +2636,7 @@ __metadata:
languageName: node
linkType: hard
"pretty-hrtime@npm:^1.0.0":
"pretty-hrtime@npm:^1.0.0, pretty-hrtime@npm:^1.0.3":
version: 1.0.3
resolution: "pretty-hrtime@npm:1.0.3"
checksum: bae0e6832fe13c3de43d1a3d43df52bf6090499d74dc65a17f5552cb1a94f1f8019a23284ddf988c3c408a09678d743901e1d8f5b7a71bec31eeeac445bef371
@ -2647,6 +2656,7 @@ __metadata:
dependencies:
"@types/node": ^16.11.6
chalk: ^4.1.2
execution-time: ^1.4.1
gulp: ^4.0.2
gulp-cli: ^2.3.0
gulp-rename: ^2.0.0